C Files - Part II 
Author Message
 C Files - Part II

I'm teaching myself C since Sept. I'm trying to create a file with
records and read that file into a structure array and print to screen.

A few weeks back, I posted the write prog here - to get some help on.
(Many thanks again for those who pointed that I should use a fwrite
instead of a printf with structures). Now it works.  

Basically the write file prog reads in a structure via user prompts and
writes it to a disk file.  The structure consists of a ball player and
his stats.  This is done 15 times.  Below is the prog:

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

Quote:
} ;

main()
{
 int testfile(),input();

 testfile();
 input();
 fclose(fptr);
 return 0;

Quote:
}

input()
{
 struct teamstruct playerec;
 int count, index, pos, length;

 clrscr();
 for (count=0; count < 15; count++)
  {
   printf("\n Please enter Player Name:  ");
   gets  (playerec.name);
   length = strlen(playerec.name);
   for (; length != 19; length++)
    {
     playerec.name[length] = ' ';
    }
   playerec.name[19] = '\0';
   printf("\n Please enter player's At Bats:  ");
   scanf ("%d", &playerec.atbats);
   printf("\n Please enter number of Singles:  ");
   scanf ("%d", &playerec.singles);
   printf("\n Please enter number of Doubles:  ");
   scanf ("%d", &playerec.doubles);
   printf("\n please enter number of triples:  ");
   scanf ("%d", &playerec.triples);
   printf("\n Please enter number of Home Runs:  ");
   scanf ("%d", &playerec.hr);
   for (index=0; index < 4; index++)
    {
     pos=index;
     printf("\n Please enter position #%d:  ", ++pos);
     scanf ("%d", &playerec.position[index]);
    }
   fwrite(&playerec,sizeof(playerec),1,fptr);
   getchar();
   clrscr();
   fflush(fptr);
  }
return 0;

Quote:
}

testfile()
{
   fptr = fopen("c:\\team.dat", "w");
   if (fptr == 0)
   {
    printf("\n\n\a OPEN FILE ERROR!");
   }
 return 0;

Quote:
}

The read file prog attempts to read from the disk file into a structure
array and print the array to the screen.  Below is this prog:

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

Quote:
} ;

main()
{
 int testfile(),output(), print(), header();

 testfile();
 output();
 header();
 print();
 fclose(fptr);
 return 0;

Quote:
}

testfile()
{
   fptr = fopen("c:\\team.dat", "r");
   if (fptr == 0)
   {
    printf("\n\n\a OPEN FILE ERROR!");
   }
 return 0;

Quote:
}

output()
{
 int count, ctr, index, pos;
 struct teamstruct playerec[15];

 for (count=0; count < 15; count++)
  {
   fread(&playerec[count],sizeof(playerec[0]),1,fptr);
  }
return 0;

Quote:
}

header()
{
 clrscr();
 printf("                                                      
Home\n");
 printf("    Player Name      At Bats  Singles Doubles Triples");
 printf("  Runs    Positions");
 printf("\n    --------------------------------");
 printf("------------------------------------  ");
 printf("\n");
 return 0;

Quote:
}

print()
{
 int index;
 struct teamstruct playerec[15];

 printf("\n");
 for (index=0; index < 15; index++)
  {
   printf("%s\t",playerec[index].name);
   printf("%d\t",playerec[index].atbats);
   printf("%d\t",playerec[index].singles);
   printf("%d\t",playerec[index].doubles);
   printf("%d\t",playerec[index].triples);
   printf("%d\t",playerec[index].hr);
   for (ctr=0; ctr < 4; ctr++)
    {
     printf("%d ",playerec[index].position[ctr]);
    }
   printf("\n");
  }
 return 0;

Quote:
}

The read file prog works up to the fifth record. Part of the player
structure consists of a position array of 4 int values. Each player can
play up to 4 positions. This array uses baseball notation - 1 = pitcher,
2 = catcher and so on..  It is here that I get something strange. I get
the following (This is just a partial listing):

    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       ***ERROR  
Dave Parker             637     140     44      8       21      7 8 3
6          IS
Dave Winfield           615     111     29      7       2       1 2 -360
2  <----HERE!
         -224    9611    1       -360    2       476 419 2573
11565            
--------------------------------  Doubles Triples
Home                        
        11565   11565   11565   11565   11565   11565 8224 28484
25205          
les Triples
Home                                                              
        271     53      512     768     1536    1280 1024 512 19712  

For some reason, I get the integers 1, 2, -360 and 2 for the fifth
player in the positions col.  This happens every time! No matter what
positions I put! You can see that from here, everything else is just a
mess!  When I wrote the data to the file, I did a Run Trace w/Watches
and everything was fine!  When I did  a Run Trace for the read
w/Watches, it reads 1, 2, -360 and 2 for the fifth record from the
array.  I can't seem to figure where these numbers came from?

 I know that this may be a lot, but if anybody has any hints, tips or
pointers, I would be grateful. Right now my brain is fried. I think I
need some time away from this.  Maybe I'm not seeing/thinking clearly.
Hopefuuly in a day or two I can find the solution.

Thank you for your wisdom, time and consideration..

George



Fri, 16 Apr 1999 03:00:00 GMT  
 C Files - Part II

Quote:

>I'm teaching myself C since Sept. I'm trying to create a file with
>records and read that file into a structure array and print to screen.

[snip!]

Quote:
>   fptr = fopen("c:\\team.dat", "w");
>   fptr = fopen("c:\\team.dat", "r");

                                  ^

These would be best as "wb" and "rb". With files opened in text mode (the
default) under DOS, any 0x0D bytes encountered in the output stream get
converted on the fly to 0x0D 0x0A, because the '\n' character is translated
to a newline and carriage return sequence, the required line terminators
for DOS machines.

For other operating systems, there's also a potential problem if you write
the end of file marker.

Quote:
> I know that this may be a lot, but if anybody has any hints, tips or
>pointers, I would be grateful. Right now my brain is fried. I think I
>need some time away from this.  Maybe I'm not seeing/thinking clearly.
>Hopefuuly in a day or two I can find the solution.

There could be another bug somewhere, as I've not tried running the code,
but reading and writing binary files in text mode under DOS is just asking
for trouble.

Quote:
>Thank you for your wisdom, time and consideration..

HTH

Quote:
>George

Ken

PS: Should something about this be in the FAQ? I see this opening DOS files
in text instead of binary problem at least once a month.
--
  ___        ,_,         ___      Ken Nicolson
 (o,o)      (o,o)    ,,,(o,o),,,

 -"-"-      -"-"-       -"-"-           +44 131 555 6055

hoots about my opinions           Web:  http://www.ride.demon.co.uk



Sat, 17 Apr 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. reading data from a text file part II

2. Reading File - Part II

3. File handle - PART II

4. Newbie: separate big .cs file into small .cs files

5. C-oriented text for intro CS II

6. Trashing of memory...part II. Src included

7. graphics and sound part II

8. Events Part II

9. pseudo code part II

10. Dynamic Memory Part II

11. Part II: alternative to converting between big/little endian

12. looking for a C phone utility (Part II)

 

 
Powered by phpBB® Forum Software