29 Input/output library [input.output]

29.7 Formatting and manipulators [iostream.format]

29.7.4 Input streams [input.streams]

29.7.4.3 Unformatted input functions [istream.unformatted]

Each unformatted input function begins execution by constructing an object of class sentry with the default argument noskipws (second) argument true.
If the sentry object returns true, when converted to a value of type bool, the function endeavors to obtain the requested input.
Otherwise, if the sentry constructor exits by throwing an exception or if the sentry object returns false, when converted to a value of type bool, the function returns without attempting to obtain any input.
In either case the number of extracted characters is set to 0; unformatted input functions taking a character array of nonzero size as an argument shall also store a null character (using charT()) in the first location of the array.
If an exception is thrown during input then ios_­base​::​badbit is turned on306 in *this's error state.
(Exceptions thrown from basic_­ios<>​::​clear() are not caught or rethrown.)
If (exceptions()&badbit) != 0 then the exception is rethrown.
It also counts the number of characters extracted.
If no exception has been thrown it ends by storing the count in a member object and returning the value specified.
In any event the sentry object is destroyed before leaving the unformatted input function.
streamsize gcount() const;
Effects: None.
This member function does not behave as an unformatted input function (as described above).
Returns: The number of characters extracted by the last unformatted input member function called for the object.
int_type get();
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, extracts a character c, if one is available.
Otherwise, the function calls setstate(failbit), which may throw ios_­base​::​failure ([iostate.flags]).
Returns: c if available, otherwise traits​::​eof().
basic_istream<charT, traits>& get(char_type& c);
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, extracts a character, if one is available, and assigns it to c.307
Otherwise, the function calls setstate(failbit) (which may throw ios_­base​::​failure ([iostate.flags])).
Returns: *this.
basic_istream<charT, traits>& get(char_type* s, streamsize n, char_type delim);
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, extracts characters and stores them into successive locations of an array whose first element is designated by s.308
Characters are extracted and stored until any of the following occurs:
  • n is less than one or n - 1 characters are stored;
  • end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit));
  • traits​::​eq(c, delim) for the next available input character c (in which case c is not extracted).
If the function stores no characters, it calls setstate(failbit) (which may throw ios_­base​::​​failure ([iostate.flags])).
In any case, if n is greater than zero it then stores a null character into the next successive location of the array.
Returns: *this.
basic_istream<charT, traits>& get(char_type* s, streamsize n);
Effects: Calls get(s, n, widen('\n')).
Returns: Value returned by the call.
basic_istream<charT, traits>& get(basic_streambuf<char_type, traits>& sb, char_type delim);
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, extracts characters and inserts them in the output sequence controlled by sb.
Characters are extracted and inserted until any of the following occurs:
  • end-of-file occurs on the input sequence;
  • inserting in the output sequence fails (in which case the character to be inserted is not extracted);
  • traits​::​eq(c, delim) for the next available input character c (in which case c is not extracted);
  • an exception occurs (in which case, the exception is caught but not rethrown).
If the function inserts no characters, it calls setstate(failbit), which may throw ios_­base​::​​failure ([iostate.flags]).
Returns: *this.
basic_istream<charT, traits>& get(basic_streambuf<char_type, traits>& sb);
Effects: Calls get(sb, widen('\n')).
Returns: Value returned by the call.
basic_istream<charT, traits>& getline(char_type* s, streamsize n, char_type delim);
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, extracts characters and stores them into successive locations of an array whose first element is designated by s.309
Characters are extracted and stored until one of the following occurs:
  1. 1.end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit));
  2. 2.traits​::​eq(c, delim) for the next available input character c (in which case the input character is extracted but not stored);310
  3. 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.311
If the function extracts no characters, it calls setstate(failbit) (which may throw ios_­base​::​​failure ([iostate.flags])).312
In any case, if n is greater than zero, it then stores a null character (using charT()) into the next successive location of the array.
Returns: *this.
[Example
:
#include <iostream>

int main() {
  using namespace std;
  const int line_buffer_size = 100;

  char buffer[line_buffer_size];
  int line_number = 0;
  while (cin.getline(buffer, line_buffer_size, '\n') || cin.gcount()) {
    int count = cin.gcount();
    if (cin.eof())
      cout << "Partial final line";     // cin.fail() is false
    else if (cin.fail()) {
      cout << "Partial long line";
      cin.clear(cin.rdstate() & ~ios_base::failbit);
    } else {
      count--;                          // Don't include newline in count
      cout << "Line " << ++line_number;
    }
    cout << " (" << count << " chars): " << buffer << endl;
  }
}
โ€” end example
]
basic_istream<charT, traits>& getline(char_type* s, streamsize n);
Returns: getline(s, n, widen('\n'))
basic_istream<charT, traits>& ignore(streamsize n = 1, int_type delim = traits::eof());
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, extracts characters and discards them.
Characters are extracted until any of the following occurs:
  • n != numeric_­limits<streamsize>​::​max() and n characters have been extracted so far
  • end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit), which may throw ios_­base​::​failure ([iostate.flags]));
  • traits​::​eq_­int_­type(traits​::​to_­int_­type(c), delim) for the next available input character c (in which case c is extracted).
Remarks: The last condition will never occur if traits​::​eq_­int_­type(delim, traits​::​eof()).
Returns: *this.
int_type peek();
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, reads but does not extract the current input character.
Returns: traits​::​eof() if good() is false.
Otherwise, returns rdbuf()->sgetc().
basic_istream<charT, traits>& read(char_type* s, streamsize n);
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, if !good() calls setstate(failbit) which may throw an exception, and return.
Otherwise extracts characters and stores them into successive locations of an array whose first element is designated by s.313
Characters are extracted and stored until either of the following occurs:
  • n characters are stored;
  • end-of-file occurs on the input sequence (in which case the function calls setstate(failbit | eofbit), which may throw ios_­base​::​failure ([iostate.flags])).
Returns: *this.
streamsize readsome(char_type* s, streamsize n);
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, if !good() calls setstate(failbit) which may throw an exception, and return.
Otherwise extracts characters and stores them into successive locations of an array whose first element is designated by s.
If rdbuf()->in_­avail() == -1, calls setstate(eofbit) (which may throw ios_­base​::​failure ([iostate.flags])), and extracts no characters;
  • If rdbuf()->in_­avail() == 0, extracts no characters
  • If rdbuf()->in_­avail() > 0, extracts min(rdbuf()->in_­avail(), n)).
Returns: The number of characters extracted.
basic_istream<charT, traits>& putback(char_type c);
Effects: Behaves as an unformatted input function (as described above), except that the function first clears eofbit.
After constructing a sentry object, if !good() calls setstate(failbit) which may throw an exception, and return.
If rdbuf() is not null, calls rdbuf()->sputbackc(c).
If rdbuf() is null, or if sputbackc returns traits​::​eof(), calls setstate(badbit) (which may throw ios_­base​::​failure ([iostate.flags])).
[Note
:
This function extracts no characters, so the value returned by the next call to gcount() is 0.
โ€” end note
]
Returns: *this.
basic_istream<charT, traits>& unget();
Effects: Behaves as an unformatted input function (as described above), except that the function first clears eofbit.
After constructing a sentry object, if !good() calls setstate(failbit) which may throw an exception, and return.
If rdbuf() is not null, calls rdbuf()->sungetc().
If rdbuf() is null, or if sungetc returns traits​::​eof(), calls setstate(badbit) (which may throw ios_­base​::​failure ([iostate.flags])).
[Note
:
This function extracts no characters, so the value returned by the next call to gcount() is 0.
โ€” end note
]
Returns: *this.
int sync();
Effects: Behaves as an unformatted input function (as described above), except that it does not count the number of characters extracted and does not affect the value returned by subsequent calls to gcount().
After constructing a sentry object, if rdbuf() is a null pointer, returns -1.
Otherwise, calls rdbuf()->pubsync() and, if that function returns -1 calls setstate(badbit) (which may throw ios_­base​::​failure ([iostate.flags]), and returns -1.
Otherwise, returns zero.
pos_type tellg();
Effects: Behaves as an unformatted input function (as described above), except that it does not count the number of characters extracted and does not affect the value returned by subsequent calls to gcount().
Returns: After constructing a sentry object, if fail() != false, returns pos_­type(-1) to indicate failure.
Otherwise, returns rdbuf()->pubseekoff(0, cur, in).
basic_istream<charT, traits>& seekg(pos_type pos);
Effects: Behaves as an unformatted input function (as described above), except that the function first clears eofbit, it does not count the number of characters extracted, and it does not affect the value returned by subsequent calls to gcount().
After constructing a sentry object, if fail() != true, executes rdbuf()->pubseekpos(pos, ios_­base​::​in).
In case of failure, the function calls setstate(failbit) (which may throw ios_­base​::​failure).
Returns: *this.
basic_istream<charT, traits>& seekg(off_type off, ios_base::seekdir dir);
Effects: Behaves as an unformatted input function (as described above), except that the function first clears eofbit, does not count the number of characters extracted, and does not affect the value returned by subsequent calls to gcount().
After constructing a sentry object, if fail() != true, executes rdbuf()->pubseekoff(off, dir, ios_­base​::​in).
In case of failure, the function calls setstate(​failbit) (which may throw ios_­base​::​failure).
Returns: *this.
This is done without causing an ios_­base​::​failure to be thrown.
โฎฅ
Note that this function is not overloaded on types signed char and unsigned char.
โฎฅ
Note that this function is not overloaded on types signed char and unsigned char.
โฎฅ
Note that this function is not overloaded on types signed char and unsigned char.
โฎฅ
Since the final input character is โ€œextractedโ€, it is counted in the gcount(), even though it is not stored.
โฎฅ
This allows an input line which exactly fills the buffer, without setting failbit.
This is different behavior than the historical AT&T implementation.
โฎฅ
This implies an empty input line will not cause failbit to be set.
โฎฅ
Note that this function is not overloaded on types signed char and unsigned char.
โฎฅ