Struct / File i/o help
From when I started learning C I''ve had a telephone directory program that I''ve been continually revising as I learn more. I''ve just been learning file i/o and structures.
I have a nested structure as such:-
struct address
{
char numstreet[20];
char town[20];
char postcode[20];
};
struct people
{
char name[20];
int areacode;
char number[20];
address add;
} ;
Quite straightforward. In main I''ve declared
people person[100];
Now, for some reason I thought that when it comes to saving the directory I could save each individual person as follows:-
I open the file for binary write and thought I could then write the whole structure in one easy step as such (i is the counter):-
fwrite(person, sizeof person, 1, directory);
and then read in as follows:
fread(person,sizeof person, 1, directory);
It doesn''t seem to work though!!!
Can someone tell me why it doesn''t work. Where is my flawed logic? I thought you''d be able to write one instance of a structure as a whole.
Help please! </i>
You can write the whole lot out in one go by doing
fwrite ( person, sizeof( person ), 1, directory ) ;
or just the one record with
fwrite ( &person, sizeof( people ), 1, directory );<br><br>the fread arguments are just the same.<br><br> <br><br><SPAN CLASS=editedby>[edited by - Paradigm Shifter on September 6, 2002 8:08:06 AM]</SPAN>
fwrite ( person, sizeof( person ), 1, directory ) ;
or just the one record with
fwrite ( &person, sizeof( people ), 1, directory );<br><br>the fread arguments are just the same.<br><br> <br><br><SPAN CLASS=editedby>[edited by - Paradigm Shifter on September 6, 2002 8:08:06 AM]</SPAN>
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement