写入文件

huangapple go评论54阅读模式
英文:

Writting on a file

问题

I have translated the code-related part as you requested. Here it is:

//C Library
#include <stdlib.h>
#include <stdio.h>
//Header Files
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> //for close

int* sala;
int aforo;

void crea_sala(int capacidad){
    sala = (int*) malloc(capacidad* sizeof(int));
    aforo = capacidad;

    for(int i = 0; i < capacidad; i++){
        *(sala+i) = -1;
    }
}

int reserva_asiento(int id){
    for(int i = 1; i <= aforo; i++){
        if(*(sala+i-1) == id){
            return -2;
        }
    }
    for(int i = 1; i <= aforo; i++){
        if(*(sala+i-1) == -1){
            *(sala+i-1) = id;
            return i;
        }
    }

    return -1;
}

int libera_asiento(int asiento){
    int devuelve = *(sala+asiento-1);
    *(sala+asiento-1) = -1;
    return devuelve;
}

int estado_asiento(int asiento){
    if(*(sala+asiento-1) == -1){
        return 0;
    }else if(*(sala+asiento-1) > 0){
        return *(sala+asiento-1);
    }else{
        return -1;
    }
}

int asientos_libres(){
    int contador = 0;
    for(int i = 1; i <= aforo; i++){
        if(estado_asiento(i) == 0){
            contador++;
        }
    }
    return contador;
}

int asientos_ocupados(){
    return aforo - asientos_libres();
}

int capacidad(){
    return aforo;
}

void elimina_sala(){
    free(sala);
}

int encuentra_asiento(int id){
    for(int i = 0; i < aforo; i++){
        if(estado_asiento(i) == id){
            return i+1;
        }
    }
    return -1;
}

// THIS IS THE FUNCTION:
int guarda_estado_sala(const char* ruta_fichero){
    int fd = creat(ruta_fichero, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    for(int i=0; i < aforo; i++){
        write(fd, (char) *(sala+i), sizeof(char)); 
    }
    close(fd);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include "../headers/sala.h"

void main(){
    crea_sala(10);
    reserva_asiento(10);
    guarda_estado_sala("saladeprueba.txt");
    elimina_sala();
}
英文:

im working on a project for college and I have a problem. I have to manage a theatre room. Everything is fine about it, but now I have to copy the state of the room to a file. When I call the function guarda_estado_sala(), which should print the ids of the clients in the file (stored at the variable sala which is a pointer), it doesn't do anything at all. I even added a printf inside the function, but it doesn't work, any clues on why?

Here is the code (the function I need to change is marked with a comment):

//Cuerpo biblioteca
#include &lt;stdlib.h&gt; 
#include &lt;stdio.h&gt;
//cabeceras archivos
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;unistd.h&gt; //para close
int* sala;
int aforo;
void crea_sala(int capacidad){
sala = (int*) malloc(capacidad* sizeof(int));
aforo = capacidad;
for(int i = 0; i&lt;capacidad; i++){
*(sala+i) = -1;
}
}
int reserva_asiento(int id){
for(int i = 1; i &lt;= aforo; i++){
if(*(sala+i-1) ==id){
return -2;
}
}
for(int i = 1; i &lt;= aforo; i++){
if(*(sala+i-1) ==-1){
*(sala+i-1) = id;
return i;
}
}
return -1;
}
int libera_asiento(int asiento){
int devuelve = *(sala+asiento-1);
*(sala+asiento-1) = -1;
return devuelve;
}
int estado_asiento(int asiento){
if(*(sala+asiento-1) == -1){
return 0;
}else if(*(sala+asiento-1) &gt; 0){
return *(sala+asiento-1);
}else{
return -1;
}
}
int asientos_libres(){
int contador = 0;
for(int i = 1; i&lt;=aforo; i++){
if(estado_asiento(i) == 0){
contador++;
}
}
return contador;
}
int asientos_ocupados(){
return aforo-asientos_libres();
}
int capacidad(){
return aforo;
}
void elimina_sala(){
free(sala);
}
int encuentra_asiento(int id){
for(int i = 0; i&lt;aforo; i++){
if(estado_asiento(i) == id){
return i+1;
}
}
return -1;
}
// THIS IS THE FUNCTION:
int guarda_estado_sala(const char* ruta_fichero){
int fd = creat(ruta_fichero, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
for(int i=0; i&lt;aforo; i++){
write(fd, (char) *(sala+i), sizeof(char)); 
}
close(fd);
return 0;
}
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &quot;../cabeceras/sala.h&quot;
void main(){
crea_sala(10);
reserva_asiento(10);
guarda_estado_sala(&quot;saladeprueba.txt&quot;);
elimina_sala();
}

答案1

得分: 2

这是错误的多种方式:

write(fd, (char) *(sala+i), sizeof(char)); 
  • (char) *(sala+i) 这里对 sala[i] 进行了解引用,并将其转换为 char。你应该提供一个 void*
  • sizeof(char) 不是正确的大小,因为你要写入的是 int。修正后,应该是这样的:
for(int i = 0; i < aforo; i++) {
    write(fd, sala + i, sizeof *sala);
}

但是,并没有必要使用循环。可以一次性将所有内容写入:

write(fd, sala, aforo * sizeof *sala);
英文:

This is wrong in multiple ways:

write(fd, (char) *(sala+i), sizeof(char)); 
  • (char) *(sala+i) This dereferences sala[i] and cast it to a char. You should supply a void*.
  • sizeof(char) is not the correct size since you are writing ints. Corrected, it'd look like this:
for(int i = 0; i &lt; aforo; i++) {
    write(fd, sala + i, sizeof *sala);
}

but, there's no reason to do it in a loop. Just write it all at once:

write(fd, sala, aforo * sizeof *sala);

huangapple
  • 本文由 发表于 2023年4月7日 01:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定