reading consecutive array elements of a structure and printing it immediately until it reaches end of file in c -


i making simple game using c programming our project. information of players (player number such 1 2 or 3, name of player, , score) stored in text file through structures. want "load" information of previous players before game starts current player knows his/her player number getting player numbes file , printing out. using while(!feof(fp)) i'm getting trouble because prints first player number. please me.

while(!feof(fp)) { fp = fopen("scores.txt","a+"); fscanf(fp, "%i \n", &p[load].nth); printf ("loading player no. %i...\n", p[load].nth); fscanf(fp, "%s \n", p[load].name); fscanf(fp, "%i \n", &p[load].score); load++; }  count=load; p[count].nth=count; printf("you player no. %i", p[count].nth); 

your code has few errors.

  1. you can't use feof(fp) before opening file , assigning value fp. that's undefined behavior.
  2. you shouldn't use "a+" mode when opening read, means "read append" not you're after.
  3. you shouldn't use feof() anyway: it's purpose not figure out when stop reading, it's figuring out after fact why reading failed.
  4. you don't need & when argument string.

it seems there off-by-one indexing problem way handle count.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -