return value of getline ?

This is a discussion on return value of getline ? within the C++ Programming forums, part of the General Programming Boards category; In books and websites you frequently see examples where the getline function from <string> is used for example like this: ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    65

    return value of getline ?

    In books and websites you frequently see examples where the getline function from <string> is used for example like this:
    Code:
    while (getline(istream, str))
        cout << str << endl;
    I was wondering what value getline actually returns so that the loop stops? All references I can find say that it returns the stream it was reading, but how can a stream suddenly become 0 or false or anything like that?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It returns a reference to the istream object.

    but how can a stream suddenly become 0 or false or anything like that
    In C++, you can program an object to behave anyway you want. The ostream class is programmed so that when on ostream object is in a boolean context, it evaluates to false if any error flags have been set for the object, like eof. Otherwise, it evalutes to true.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Ahh, that's interesting, thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> it evaluates to false if any error flags have been set for the object, like eof.

    Technically, it evaluates to false if failbit or badbit is set. It does not return false if eof is set by itself. In many circumstances the eofbit and failbit are set at the same time when one final attempt to read fails because the end of the file is reached.

    However, in some cases, especially with getline above, the last call to getline will retrieve some data and consume the newline at the end of the file. This will cause the eofbit to be set, but because data was consumed and getline succeeded the failbit won't be set yet. This is what you'd want though, because you want to process that last piece of data. The next time through the read will fail completely because there is nothing left in the stream, and so both failbit and eofbit will be set causing the loop to end.

    The details are confusing, but in practice just using the return value of the stream as the example above does is usually the best way to go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21