fwrite,fread,File & Structures 
Author Message
 fwrite,fread,File & Structures

I'm having a slight problem with writing and reading a structure to a
file.  In the write and read program, I define the file pointer and
record structure:

FILE *fptr;
struct teamstruct
{
 char name[20];
 int  atbats;
 int  singles;
 int  doubles;
 int  triples;
 int  hr;
 int  position[4];

Quote:
} ;

The structure var is:   struct teamstruct playerec;
Again in both, I open file:

testfile()
{
   fptr = fopen("a:\\team.dat", "w");  ---> I use a "r" in read prg.
   if (fptr == 0)
   {
    printf("\n\n\a OPEN FILE ERROR!");
   }
 return 0;

Quote:
}

In the write program, the fwrite function is as follows:

 fwrite(&playerec,sizeof(playerec),1,fptr);

In the read file prog, the fread is as follows:

 fread(&playerec[count],sizeof(playerec[0]),1,fptr);

In the write prog, I enter 6  records (playerec) within a loop.  In the
read prog I read the records off the disk file into a structure array
and print it.  HERE's THE OUTPUT:

    Player Name      At Bats  Singles Doubles Triples  Runs    Positions

--------------------------------------------------------------------        

Carl Yastrzemski        558     103     27      3       28      7 8 9
3        
Gary Carter             522     94      29      2       31      2 7 8
9        
Bobby Bonds             591     91      23      9       37      7 8 9
4        
Dave Parker             637     140     44      8       21      7 8 3
6        
Dave Winfield           615     111     29      7       2       1 2 -360
2      
         -224    9611    1       -360    2       476 419 2573
11565            
--------------------------------  Doubles Triples
Home                        
        11565   11565   11565   11565   11565   11565 8224 28484 25205

Everything is fine until the fifth record (Winfield)! It starts with the
Home Run Col.  There should be a 29, but only a 2 shows.  Also, the
position notation displays 1, 2, -360 and 2 which is wrong.  This is
displayed every time no matter what positions I enter here.  And no 6th
record is shown.

I think I should use the fwrite and fread to w/r structures to a file. I
am new to C programming and the two books I have don't cover files that
much.  Am I doing this right?  Any hints, tips or suggestions are
welcome... Time to go back and burn my brain some more.  Thank yo for
your wisdom in advance!

George



Sun, 18 Apr 1999 03:00:00 GMT  
 fwrite,fread,File & Structures

Quote:

>I'm having a slight problem with writing and reading a structure to a
>file.  In the write and read program, I define the file pointer and
>record structure:

>FILE *fptr;
>struct teamstruct
>{
> char name[20];
> int  atbats;
> int  singles;
> int  doubles;
> int  triples;
> int  hr;
> int  position[4];
>} ;

>The structure var is:   struct teamstruct playerec;
>Again in both, I open file:

>testfile()
>{
>   fptr = fopen("a:\\team.dat", "w");  ---> I use a "r" in read prg.

If you're read/writing binary data you need to open the file in binary
mode (the default is text mode). Use "wb" or "rb" as the 2nd argument to
fopen() as appropriate.

--
-----------------------------------------


-----------------------------------------



Mon, 19 Apr 1999 03:00:00 GMT  
 fwrite,fread,File & Structures

Quote:

> <snip>

> testfile()
> {
>    fptr = fopen("a:\\team.dat", "w");  ---> I use a "r" in read prg.
>    if (fptr == 0)
>    {

<snip>

Since your structure contains integer information, try opening the file
with a "b" in the mode parameter (i.e. "wb" and "rb"). This indicates
that you are accessing a binary file as opposed to a text file.



Sat, 24 Apr 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

2. setvbuf, fwrite & fread

3. fwrite() & fread() question

4. FREAD & FWRITE

5. fwrite & fread

6. Copy files with fread/fwrite

7. Portable length-of-file && fread()

8. Portable length-of-file && fread()

9. fread, fwrite probs

10. Please Help (fread, fwrite)

11. fwrite(), fread() portability.

12. problem with fwrite / fread

 

 
Powered by phpBB® Forum Software