英文:
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 <stdlib.h>
#include <stdio.h>
//cabeceras archivos
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> //para 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 "../cabeceras/sala.h"
void main(){
crea_sala(10);
reserva_asiento(10);
guarda_estado_sala("saladeprueba.txt");
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 dereferencessala[i]
and cast it to achar
. You should supply avoid*
.sizeof(char)
is not the correct size since you are writingint
s. Corrected, it'd look like this:
for(int i = 0; i < 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论