how do you write an array of structures to a file, i tried this
fwrite(&seats[x], sizeof seats[x], 1, fl);
where fl is the file handler and seats is an array of structures.
Printable View
how do you write an array of structures to a file, i tried this
fwrite(&seats[x], sizeof seats[x], 1, fl);
where fl is the file handler and seats is an array of structures.
fwrite( array, sizeof( struct structtype ), array_members, file );
Quzah.
This is how I would do it.
Set a max for your array.
I apologize if thats wrong. I'm learning C myself. However thats how i'd go about writing an array to a file. :)Code:#define MAX_SEATS 10
for (i = 0, i < MAX_SEATS, i++)
{
fprintf(fl, "%s", seats[i]);
}
thanx, but I am still getting seg faults when trying to write the structure to the hdd. here is what the structure looks like
struct nm {
char first[20];
char last[20];
};
struct layout {
short assign;
struct nm name;
} seats[12];
I tried this
fwrite(&seats, sizeof(struct layout), 12, fl);
where fl is the file handler, can someone help?
fwrite(seats, sizeof(struct layout), 12, fl);
The above should work. You were using "&seats" and thereby passing the address of the address of seats to fwrite(), if I am correct. ;)
[edit]
Ah, nevermind. "&seats" is correct.
Some kind of "Alzheimer jr." attack. :D
[/edit]
oh, i forgot to assign the fl to the return of fopen, i did this
if((fopen("/home/bane/chris.dat", "wb"))==NULL) {
printf("Can't write ~/chris.dat!");
exit(0);
}
sorry to waste your time, :)
See, always post enough code to let us know what's happening -- and don't forget code tags :)