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.
- you can't use
feof(fp)
before opening file , assigning valuefp
. that's undefined behavior. - you shouldn't use
"a+"
mode when opening read, means "read append" not you're after. - you shouldn't use
feof()
anyway: it's purpose not figure out when stop reading, it's figuring out after fact why reading failed. - you don't need
&
when argument string.
it seems there off-by-one indexing problem way handle count
.
Comments
Post a Comment