0

If the ifstream::getline call does not find the delimeter I know it sets failbit but does it also clear out the buffer or does it leave the buffer intact and just set the fail bit to let you know?

csteifel
  • 2,854
  • 6
  • 35
  • 61
  • It leaves the buffer intact, containting what was extracted before `failbit` was set. – jrok May 29 '13 at 13:20

2 Answers2

0

From here:

Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it extracts characters from its associated stream buffer object as if calling its member functions sbumpc or sgetc, and finally destroys the sentry object before returning.

It seems that the buffer is filled until there is a problem. (See DyP's comments)

Djon
  • 2,230
  • 14
  • 19
  • I'm tryint to get more information on this behavior but from what I understand I'd say it leaves it intact. – Djon May 29 '13 at 13:22
  • I don't think so?? The sentry object is to check whether the stream is good or not; `getline` extracts **until** some condition like end-of-file occurs; see [cppreference](http://en.cppreference.com/w/cpp/io/basic_istream/getline) or Standard [istream.unformatted]/18, "Characters are extracted and stored until one of the following occurs:" – dyp May 29 '13 at 13:23
  • The `badbit` (and with it, the `basic_ios::bad` function) does not indicate an end-of-file (-> `eofbit`). – dyp May 29 '13 at 13:36
0

There seems to be some confusion about the different states of an input stream (and rightly so, they are confusing):

C++ Standard, table 124

  • badbit indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error from a file);
  • eofbit indicates that an input operation reached the end of an input sequence;
  • failbit indicates that an input operation failed to read the expected characters, or that an output operation failed to generate the desired characters.

That is, failbit is set when basic_istream::getline(char_type* s, std::streamsize count, char_type delim) extracts count-1 characters without finding the delimiter (-1 for the terminating \0 stored). This does not indicate the stream is bad, but it rather indicates that getline has failed to find the delimiter.

Description of basic_istream::getline in the C++ Standard: [istream.unformatted]/18

  • Effects: [...] After constructing a sentry object [= preparation for input and error checks], extracts characters and stores them into successive locations of an array whose first element is designated by s. Characters are extracted and stored until one of the following occurs:
    1. end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit));
    2. traits::eq(c, delim) [= delimiter found] for the next available input character c (in which case the input character is extracted but not stored);
    3. n is less than one or n - 1 characters are stored (in which case the function calls setstate(failbit)). [...]
  • These conditions are tested in the order shown.
  • If the function extracts no characters, it calls setstate(failbit) [...]
  • In any case, if n is greater than zero, it then stores a null character [...] into the next successive location of the array.

[emphasis and omissions mine]

dyp
  • 38,334
  • 13
  • 112
  • 177