This is my first question here and I hope it is a sensible one. Here it goes:
I use a very simple loop to read value from a file:
int i=0;
int cmp;
FILE *file = fopen(file_name,"rb");
while (cmp = fscanf(file,"%f %f %f %f %f", &Gr[i], &dr[i], &eta[i], &tau[i], &v[i]) != EOF)
{
printf("Prop=%0.4f %0.4f %0.4f %0.4f %0.4f\n%i\n%i\n", Gr[i], dr[i], eta[i], tau[i], v[i], EOF, cmp);
i++;
}
printf("EOF=%i,File state=%i\n",cmp, EOF);
fclose(file)
I have been searching around the forum and man fscanf about information on return values of the function and what I found is that fscanf returns the number of successfully matched values. However while the loop is being executed, the value of cmp is equal to 1, rather than 5. The sample file that I use has only 4 lines with 5 columns, so when i=3 the while ends correctly, but when I print the value of cmp it is 0 and not -1 which is the alleged value of EOF. I am trying this simple snippet in an attempt to actually understand what is going on. The output of the attached code is:
Prop=-5.1300 1.1424 0.4855 1.4277 0.0578
EOF=-1,File state=1
Prop=-6.3712 1.0744 0.4082 1.3579 0.1188
EOF=-1,File state=1
Prop=-4.9538 1.4177 0.3038 1.1513 0.1428
EOF=-1,File state=1
Prop=-6.4335 1.6539 0.0863 1.8511 0.0741
EOF=-1,File state=1
File state=0, EOF=-1
The problem originated in a much larger code, where for some reason fscanf starts reading form the last line of the file. Any help will be much appreciated.