Thread: return behavior of getline()

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    return behavior of getline()

    Hello. I've been using the different forms of std::getline() in a program recently, and I can't help but notice one strange thing about how it sets error flags. I'm reading in some data using
    Code:
    istream& istream::getline(char*buf, int num, char delim = '\n')
    And it seems that if the stream reaches num chars read before a delim, it sets the fail bit. Here's a little test program using ifstream.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    ifstream group("c:\\windows\\desktop\\input.txt");
    if(!group) { cout << "can't open." << endl; return EXIT_FAILURE; }
    
    char *aunt_sally = new char[25];
    
    cout << boolalpha << "before getline: " << group.fail() << endl;
    group.getline(aunt_sally,25,'@');
    cout << "after getline: " << group.fail() << endl;
    cout << aunt_sally << endl;
    
    group.close();
    
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    
    /********input.txt***************
    This is a text-only test just to see if the damn
    thing works. Of course, as I write this, I haven't
    even compiled all of my new functions. It is likely
    that  I will get tons of error and not even use
    this test file for quite a while. Still, I'm not
    discouraged. (quite more than 25 chars)
    *********************************/
    And my perplexing output:
    Code:
    before getline: false
    after getline: true
    This is a text-only test
    Press any key to continue . . .
    Shouldn't it be "after getline: false" ? I even tried it with a '@' right there at char 25, and then the fail bit was not set. Is this normal behavior?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://www.dinkumware.com/manuals/?m...tream::getline
    In order of testing, extraction stops:

    1. at end of file
    2. after the function extracts an element that compares equal to delim, in which case the element is neither put back nor appended to the controlled sequence
    3. after the function extracts count - 1 elements

    If the function extracts no elements or count - 1 elements, it calls setstate(failbit). In any case, it returns *this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Thank you. I suppose I could have googled that one, in retrospect.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

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, 10:01 PM