-
feof issues
well to start off...
a college course i am taking has a C module on it and we just had our finnal test using file i/o, i done it one way and the lecturer said its bad and i lost 10 marks..now he seems not to bright and im really sure my way is more efficiant..here is how it went
Code:
//my way
while(!feof(fp))
{
fscanf(fp , "%d %f %d" , &amount , &cost, &rate);
i++;
}
Code:
//lecturers way
fscanf(fp , "%d %f %d" , &amount , &cost, &rate);
while(!feof(fp))
{
fscanf(fp , "%d %f %d" , &amount , &cost, &rate);
i++;
}
he has told me my way will crash the program if the file is empty (he seems to think u need to read from the file first before u can use feof on it) and he said it will produce garbage in the arrays
can some one please give me some opinions on this and some ways to try and get him to give me 10 points back, thanks
-
You might want to read up on this entry in the FAQ.
-
Well you could read this
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Your lecturer is correct, though his solution is a little on the clunky side
Code:
while ( fscanf(fp , "%d %f %d" , &amount , &cost, &rate) == 3 ) {
i++;
}
See - only one fscanf() call to get right, rather than two.
It also stops if the file becomes garbage as well as at end of file.
-
thanks, but wat really is the difference between mine and his..other than the fact his will read in null if the file is empty which can cause problems, and why did he take off 10 marks?
-
oh..and i should have mentioned, we had to use feof
-
Well I suggest you try yours, his and mine with an empty file
- see how many times the loop is executed
- see what values you get printed
-
> we had to use feof
Presumably to demonstrate that its use in this context is broken?
-
i wouldnt have a clue why,
i have tried to crash both and failed to do so..
i cant really figure out why i lost 10 marks when mine seems more safe and does the same thing?
-
> i have tried to crash both and failed to do so..
Well it probably won't crash
But junk data is pretty much guaranteed.
See my fscanf call - check the return result (which neither you nor your prof did)
-
-
Which is EOF
So if you had written
Code:
while(!feof(fp))
{
if ( fscanf(fp , "%d %f %d" , &amount , &cost, &rate) ) != EOF ) {
i++;
}
}
You'd be better off.
But still not as good as mine :)
-
well the thing is..its allready done and i need to find a way to convince him mine is fine and his is not as hot as he thinks,
he thinks u need to read from the file first to set the posistion i think
-
Well, considering that feof will not signal end of file until an attempt has been made to read past the EOF marker, his is "more correct" than yours, in that the first call to feof in yours won't really do anything. So technically, his is better, because it gives feof a chance to actually catch something on the first pass.
Quzah.
-
> but wat really is the difference between mine and his
Code:
FILE *fp = fopen( "empty.txt", "r" );
if ( feof( fp ) ) {
printf( "you're right\n" );
} else {
printf( "you're wrong\n" );
}
But you're both wrong in my book