I have a file with float numbers, here is an example:
0.01
0.24
0.08
0.15
0.7
0.22
0.05
0.28
0.4
0.44
0.8
0.55
Now I need to get number of all floats (in this case 12). Empty lines should be avoided.
I did this:
FILE *f, *junk;
if (MYTHREAD == 0) {
f = fopen ("dane.dat", "r");
junk = fopen ("/dev/null", "w");
for(size = 0; fscanf(f, "%f\n", junk) != EOF; ++size);
fclose(junk);
fclose(f);
}
and it returns me 128 O_o. What is wrong?
You don’t check if
fscanfdid read a float. Use the fact that it return the number of item it read.Also, don’t read to
FILE*. read tofloat*.This code should work: