eof while loop

Posted 17 January 2009 - 10:53 AM

I'm reading in a file with a name, age, height, and weight with a while loop. Whenever I run my program, it repeats the last line of the file. I know what the problem is, im just not sure how to fix it. I need to prime my while loop to stop it to repeating the last line. I think im just having a mental block or something.

Replies To: eof while loop

Re: eof while loop

Posted 17 January 2009 - 11:15 AM

The problem is that you have a space at the end of the last line or you hit return which is adding another line. Make sure there are no trailing spaces in your file text and that should solve the problem.

The reason the space is doing that is because it won't be the end of the file (you have this space left over) so it triggers another iteration of the loop where you attempt to pull in more data. Since you don't pull in more data (other than that space) it repeats all the values in the variables from the last line you read.

Re: eof while loop

Posted 17 January 2009 - 02:01 PM

the 'while not eof' construct is wrong for C++. EOF is a flag which is set in a stream after a failure occurs.
In other words, when you read the last line of a file, everything is OK. When you subsequently attempt to read 'past the end', the EOF flag is set. At which point its too late, because your check for EOF will have happened before you'd tried reading any data.

in C++ you should use constructs such as

while ( myfile >> obj )

or

while ( std::getline(myfile, mystring) )

in order to avoid these kinds of problems

If a read operation fails, your loop will see the fail condition immediately

Re: eof while loop

Posted 18 January 2009 - 04:02 PM

Bench, on 17 Jan, 2009 - 01:01 PM, said:

the 'while not eof' construct is wrong for C++. EOF is a flag which is set in a stream after a failure occurs.
In other words, when you read the last line of a file, everything is OK. When you subsequently attempt to read 'past the end', the EOF flag is set. At which point its too late, because your check for EOF will have happened before you'd tried reading any data.

in C++ you should use constructs such as

while ( myfile >> obj )

or

while ( std::getline(myfile, mystring) )

in order to avoid these kinds of problems

If a read operation fails, your loop will see the fail condition immediately

Great stuff Bench ... If you have time ... what is your take on the problems with C EOF ? That seems to be some mysterious stuff ... ? (And ... many here might appreciate your comments?)

Re: eof while loop

Posted 19 January 2009 - 01:21 PM

David W - I'm not quite sure of the question? If its whether C has the same problem, then the answer is yes - since C and C++ both use the notion of "streams" for I/O. moreover, it can be solved using the same idiom of read while there is still data to be read

fgets in C is roughly equivalent to getline in C++ - although fgets returns a char*, which will either be NULL (if the read was unsuccessful) or it'll be a pointer to the first char of mystring.
it follows that fscanf is roughly equivalent to the stream retrieval >> operator, with the caveat that fscanf returns the number of arguments which it has successfully read from a stream; Whereas the >> operator returns a reference to the stream itself instead (which in-turn might be checked for failure in an if or while).
Checking a successful call to fscanf might look something like this

int num;
while (fscanf(myfile, "%d", &num) == 1)

the EOF #define itself is just a constant, whose value signals an error or end of file if returned from a function using that stream (either its eof or error flags are currently set) - just as with C++ streams, a flag is set after a failed operation. eg,

Re: eof while loop

Posted 02 March 2010 - 03:19 PM

Bench, on 17 January 2009 - 01:01 PM, said:

the 'while not eof' construct is wrong for C++. EOF is a flag which is set in a stream after a failure occurs.
In other words, when you read the last line of a file, everything is OK. When you subsequently attempt to read 'past the end', the EOF flag is set. At which point its too late, because your check for EOF will have happened before you'd tried reading any data.

in C++ you should use constructs such as

while ( myfile >> obj )

or

while ( std::getline(myfile, mystring) )

in order to avoid these kinds of problems

If a read operation fails, your loop will see the fail condition immediately

OK, I have a question that's exactly along these lines! First, I will put in a disclaimer and note that none of this is homework related Now then, I did run into the EOF problem will writing a simple code (I'm new to C++):

So the above code should open a file, and give us the average. However because of the eof error it adds an extra line. I tried using Bench's advice, but have been unsuccessful. I'm honestly not sure how to go about implementing either of his lines into my code, so assistance would of course be greatly appreciated!