Don't use feof() to control a loop
Use
while ( fgets(buffer,sizeof(buffer),myfile) != NULL )
Hi Salem,
I completely agree with your code snippet for checking EOF as C input functions return values that can be used to test for this condition.
However, you could still use the feof() function to control a loop if you use a priming read and move the input function call to the bottom of the loop. (Sorry, I know you know this - this is just an information "tidbit" for the OP).
fgets(buf, BUFSIZ, fp);
while(!feof(fp)) {
fputs(buf, stdout);
fgets(buf, BUFSIZ, fp);
}
I wouldn't do it like this in C for the reason I stated previously, but I have used this style in other languages where getting a hold of the return value from the input function was a tad "cumbersome" and coding the loop (especially when calling the input function at the top of the loop) became messy.
Cheers,
JD