c - Need help resolving weird fscanf issue -


i trying read file line line running through while loop instructed exit once eof has been reached. reason once last line has been read , while condition checked again program freezes.

this code:

char character1; int number1;  while(fscanf(file,"%s %d",&character1, &number1) != eof){     //printf("%s %d\n",character1,number1)  } 

my files contents:

a 1 b 2 c 3 d 4 e 5 

output:

a 1 b 2 c 3 d 4 e 5 |    <---blinking terminal pointer there 

can me figure out?

edit: not opening/closing file in main(), doing in function, causing problem?

improve condition checking on while loop. fscanf() can produce more results eof or positive number. can return positive number when end-of-file occurs after conversion has begun. meaning have going wrong conversion , data still there next time loop around more data stream. stuck infinitely failing convert same failed conversion.

you looking 2 input items check fscanf() has found 2 input items in order continue looping.


Comments