Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Handling problem :S

Status
Not open for further replies.

SuperSharp

Programmer
Mar 14, 2005
9
GB
Hi there,
I am having problems reading from a file and storing the data in a struct. Basically its a league table and I don't have any problem writing to a file or even reading from it, it just doesn't seem to want to update my struct. Here is the function:

Code:
void load_table()
{
        FILE *in_file;
        int count = 0;
        in_file = fopen("league.dat", "r");
        while ( !feof(in_file) )
        {
           fscanf(in_file, "%s %d %d %d %d %d %d %d", table[count].name, &table[count].p, &table[count].w,
           &table[count].d,&table[count].l, &table[count].f, &table[count].a, &table[count].t);
           count++;
        }
        //test to see if it is reading which is it
        fclose(in_file);
        printf("\n%s\t\t%d %d %d %d %d %d %d",table  [1].name, table[1].p, table[1].w,
        table[1].d, table[1].l, table[1].f, table[1].a, table[1].t);
}

I have the struct in "global" and it seems to work for eveything else:

Code:
 struct league
{
        char name[21];
    int p,w,d,l,f,a,t;
}table[12];
the p = played, w = won, d = draw etc.
char is the name of the teams.

No matter what I do it just don't store the data from the file into the struct. Any ideas anyone? Any help would be greatly appriciated, thanks very much.

Edd =)
 
>Any ideas anyone?
What you had seemed to work for me, but I might make a couple changes:
Code:
void load_table()
{
   int i = 0;
   FILE *in_file = fopen("league.dat", "r");
   if ( in_file )
   {
      while ( fscanf(in_file, "%s %d %d %d %d %d %d %d",
                     table[i].name, &table[i].p, &table[i].w,
                     &table[i].d,   &table[i].l, &table[i].f,
                     &table[i].a,   &table[i].t) == 8 )
      {
         // test to see if it is reading which is it
         printf("%s\t\t%d %d %d %d %d %d %d\n",table  [i].name,
                table[i].p, table[i].w, table[i].d, table[i].l,
                table[i].f, table[i].a, table[i].t);
         i++;
      }
      fclose(in_file);
   }
}
 
Hello!

Thanks very much for the reply. I am still having the same issues that the table is being displayed but not stored into the struct. I have another function 'display table' and after I have loaded it, it is always blank. The load feature only seems to display what is in the file. Hmmm I really can't figure it out, if anyone could help with this that would be brilliant. Thanks,

Edd
 
Code:
#include <stdio.h>

struct league
{
   char name[21];
   int p,w,d,l,f,a,t;
};

struct league table[12];

[blue]size_t[/blue] load_table(void)
{
   [blue]size_t[/blue] i = 0;
   FILE *in_file = fopen("league.dat", "r");
   if ( in_file )
   {
      while ( fscanf(in_file, "%s %d %d %d %d %d %d %d",
                     table[i].name, &table[i].p, &table[i].w,
                     &table[i].d,   &table[i].l, &table[i].f,
                     &table[i].a,   &table[i].t) == 8 )
      {
         [blue]if ( ++i >= sizeof table / sizeof *table )
         {
            break;
         }[/blue]
      }
      fclose(in_file);
   }
   [blue]return i[/blue];
}

[blue]void print_table(size_t count)
{
   size_t i;
   for ( i = 0; i < count; ++i )
   {
      printf("%s\t\t%d %d %d %d %d %d %d\n",table  [i].name,
             table[i].p, table[i].w, table[i].d, table[i].l,
             table[i].f, table[i].a, table[i].t);
   }
}[/blue]

int main(void)
{
   size_t items = load_table();
   print_table(items);
   return 0;
}

[green]/* league.dat
TeamName 1 2 3 4 5 6 7
AnotherName 2 3 4 5 6 7 8
*/

/* my output
TeamName		1 2 3 4 5 6 7
AnotherName		2 3 4 5 6 7 8
*/[/green]
 
Thanks very much for all your help, I have got it working now. I am starting to understand all this C business (slowly) =)
Cheers again!

Edd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top