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.
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.
Hope is the first step on the road to disappointment.
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]); }
To error is human, to really foul things up requires a computer
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?
Last edited by chrismiceli; 06-26-2003 at 11:19 AM.
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.
[/edit]
Last edited by Sargnagel; 06-26-2003 at 11:58 AM.
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
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers