Re: eof() is there a better way

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 03/30/04


Date: Tue, 30 Mar 2004 22:42:00 +1000


"Satan hotmail.com>" <q9722390<NoSpam@> wrote in message
news:40692688$0$16588$5a62ac22@freenews.iinet.net.au...
| Hi,
|
| I am always having trouble with the following:
|
| while ( !read_file.eof()){
| // do something
| }

I'm sure you are, because 'eof()' only becomes true,
when you have read past it, but by then it is too late,
and you will often end up reading the last line twice
in an expression such as the above.

| Looping again when i think the file should be empty.
| It even loops when i have created a new file with
| nothing in it (editor - vi).
|
| Is there a better way to ensure you are at EOF ?

Sure there is - The following will read until 'eof()'
is reached, and then terminate:

The correct idiom to use is:

// Meaning while 'true' in an boolean context...
while( "there is data" )
       ...do something

Which would evaluate to:
    while( read_file.getline( buffer... ) )
      // do something

    while( read_file >> buffer )
      // do something

    while( std::getline( read_file, buffer ) )
      // do something

After the while loop has completed, it is good practice
to explicitly check that the file was indeed read to the
end:

if( read_file.eof() )
    // The whole file was successfully read to 'eof()'.

Cheers.
Chris Val



Relevant Pages

  • Re: PID contollers
    ... It is operating chaotically, interacting with a half dozen or so other control loops in various ways, and giving everyone heartburn. ... They tell me that I can alter the output value, manually only plus or minus 2% (less than it is swinging in automatic or cascade mode), but they really don't trust me to put the loop in manual mode, at all. ... The thing that is neat about the graphical method I describe in my tutorial, is that it gives you some simple rules to determine which term is most likely to be causing the trouble and which way it needs to change to back away, gracefully from that trouble, without having to pass through any mine fields. ...
    (sci.electronics.basics)
  • Why is recursion hard?
    ... Is it that people have trouble thinking forward, ... the remaining subproblem after writing a routine that reduces ... why don't they have trouble with loops? ... brains around recursion, or who, at least, consider it to ...
    (comp.lang.scheme)
  • Re: if, then, next - can I do that?
    ... Beginner question: ... trouble doing something like this: ... is opening each one as the For loops around. ... and therefore the next file in the array. ...
    (microsoft.public.vb.general.discussion)
  • Re: EIGHT BALL DELUXE LE HELP NEEDED
    ... They are spread out over it. ... They are little loops of wire soldered ... trouble at all with their Bally counterparts. ...
    (rec.games.pinball)
  • Re: Goalseek "may not have found a solution"
    ... Dim Ok As Boolean ... oscillatory. ... You will want to limit the number of loops here though. ... >> Tom Ogilvy ...
    (microsoft.public.excel.programming)

Loading