Advertisement

Struct / File i/o help

Started by September 06, 2002 06:57 AM
1 comment, last by SteveBe 22 years, 3 months ago
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 &#111;ne 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 &#111;n 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
Advertisement
Thanks a lot paradigm. I appreciate it.

This topic is closed to new replies.

Advertisement