FEOF issues

This is a discussion on FEOF issues within the C Programming forums, part of the General Programming Boards category; Hi Guys, Yes, I know I am not suppose to use FEOF to control a while loop but i need ...

FEOF issues

Hi Guys, Yes, I know I am not suppose to use FEOF to control a while loop but i need a suggestion of how to solve my issue.

Code:

while (!feof(in))
adjust(root, in);

In the function, a line of characters are read and picked out one by one. When the line reaches \n the function stops. Comes out to check the while loop for FEOF. There are cases where there are multiple lines this the function runs again (exactly what i want it to do). But when it does reach FEOF in the file itself, the while loop doesnt stop. Any Suggestions?

feof() can control a loop, but only if something tries to read from in before the feof() call. So that means you have to read from in before the loop starts and before the condition is checked in the loop, or you will encounter the problem with feof(). This necessary priming of your loops is the reason why it is bad to control loops with feof().

This necessary priming of your loops is the reason why it is bad to control loops with feof().

Can you show a good use of feof for loop control? To me it always seems best to do it some other way.
EDIT: Now that I think about it, you probably want to stop the input loop whether it's eof or an error, but isn't feof only true for eof? It has a counterpart, ferror() for testing the error state. They're meant to be used after a stream has stopped to help determine why it stopped.

I think the only time an error is even possible is if the file isn't locked and it's being used by another program. I didn't mean to say that it was better, I was just being factual. Pointlessly factual apparently.

Testing the returned value for EOF is not always the best way of determining when you have arrived at the end of a file - Think of the case of when you are reading a binary file and one of the characters is the EOF character.