Sunday, July 12, 2009

How do you loop fscanf until EOF in c?

for example.. number.txt: (has these contents)





1.75:2.00:3.00


2.00:5.00:7.24


3.00:6.35:1.00





--


my source code is..





#include %26lt;stdio.h%26gt;


int main()


{


FILE *infile;


double a, b, c, sum;


char d;





infile = fopen("number.txt", "r");


if(infile == NULL)


{


printf("number DOES NOT EXISTS!");


}


else


{


while(!foef(infile)) /*here's my problem*/


{


fscanf(infile, "%lf%c%lf%c%lf\n", %26amp;a,%26amp;d,%26amp;b,%26amp;d,%26amp;c); /*fscanf is used*/


printf("%f %f %f", a, b, c); /*just for checking*/


sum = a + b + c;


printf("sum is %f", sum);


}





}


fclose(infile);


return 0;


}





--





problem is..





if i put the while loop, it won't compile. it has these error message saying "undefined reference to `foef'".





if i remove the while loop, the fscanf reads only the first line.





i would like to read also the other data from line 2 and line 3 and solve their sum respectively.





so how can i loop the the fscanf?(assuming that i don't know how many lines are in the file? or maybe its safe to say to loop the fscanf until EOF?)


thanks a lot!

How do you loop fscanf until EOF in c?
try feof


it is not foef
Reply:Do this:





while (fscanf(infile, "%lf%c%lf%c%lf\n", %26amp;a,%26amp;d,%26amp;b,%26amp;d,%26amp;c) ){


.


.


.


}





fscanf should return false if it runs out of data


No comments:

Post a Comment