[istream.unformatted] (original) (raw)
31 Input/output library [input.output]
31.7 Formatting and manipulators [iostream.format]
31.7.5 Input streams [input.streams]
31.7.5.4 Unformatted input functions [istream.unformatted]
Each unformatted input function begins execution by constructing an object of type ios_base::iostate, termed the local error state, and initializing it to ios_base::goodbit.
It then creates an object of classsentrywith the default argumentnoskipws(second) argumenttrue.
If thesentryobject returnstrue, when converted to a value of typebool, the function endeavors to obtain the requested input.
Otherwise, if the sentry constructor exits by throwing an exception or if the sentry object produces false, when converted to a value of typebool, 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 (usingcharT()) in the first location of the array.
If rdbuf()->sbumpc() or rdbuf()->sgetc()returns traits::eof(), thenios_base::eofbit is set in the local error state and the input function stops trying to obtain the requested input.
If an exception is thrown during input thenios_base::badbit is set in the local error state,*this's error state is set to the local error state, and the exception is rethrown if (exceptions() & badbit) != 0.
If no exception has been thrown it stores the number of characters extracted in a member object.
After extraction is done, the input function calls setstate, which sets *this's error state to the local error state, and may throw an exception.
In any event thesentryobject 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.
If the number cannot be represented, returns numeric_limits<streamsize>::max().
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, extracts a character c, if one is available.
Otherwise,ios_base::failbit is set in the input function's local error state before setstate is called.
Returns: c if available, otherwisetraits::eof().
basic_istream& 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.277
Otherwise,ios_base::failbit is set in the input function's local error state before setstate is called.
basic_istream& 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 bys.278
Characters are extracted and stored until any of the following occurs:
- n is less than one or n - 1characters are stored;
- end-of-file occurs on the input sequence;
- traits::eq(c, delim)for the next available input character c(in which case c is not extracted).
If the function stores no characters,ios_base::failbit is set in the input function's local error state before setstate is called.
In any case, if n is greater than zero it then stores a null character into the next successive location of the array.
basic_istream& get(char_type* s, streamsize n);
Effects: Callsget(s, n, widen('\n')).
Returns: Value returned by the call.
basic_istream& 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 bysb.
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,ios_base::failbit is set in the input function's local error state before setstate is called.
basic_istream& get(basic_streambuf<char_type, traits>& sb);
Effects: Callsget(sb, widen('\n')).
Returns: Value returned by the call.
basic_istream& 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 bys.279
Characters are extracted and stored until one of the following occurs:
1. | end-of-file occurs on the input sequence; |
---|---|
2. | traits::eq(c, delim)for the next available input character c(in which case the input character is extracted but not stored);280 |
3. | n is less than one or n - 1characters are stored (in which case the function callssetstate(failbit)). |
These conditions are tested in the order shown.281
If the function extracts no characters,ios_base::failbit is set in the input function's local error state before setstate is called.282
In any case, if n is greater than zero, it then stores a null character (usingcharT()) into the next successive location of the array.
[Example 1: #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"; else if (cin.fail()) { cout << "Partial long line"; cin.clear(cin.rdstate() & ~ios_base::failbit);} else { count--; cout << "Line " << ++line_number;} cout << " (" << count << " chars): " << buffer << endl;} } — _end example_]
basic_istream& getline(char_type* s, streamsize n);
Returns: getline(s, n, widen('\n'))
basic_istream& 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::max()numeric.limitsandn characters have been extracted so far;
- end-of-file occurs on the input sequence (in which case the function callssetstate(eofbit), which may throwios_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).
[Note 1:
The last condition will never occur iftraits::eq_int_type(delim, traits::eof()).
— _end note_]
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()ifgood()isfalse.
Otherwise, returnsrdbuf()->sgetc().
basic_istream& read(char_type* s, streamsize n);
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, if!good()callssetstate(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 bys.283
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 callssetstate(failbit | eofbit), which may throwios_base::failure ([iostate.flags])).
streamsize readsome(char_type* s, streamsize n);
Effects: Behaves as an unformatted input function (as described above).
After constructing a sentry object, if!good()callssetstate(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 bys.
Ifrdbuf()->in_avail() == -1, callssetstate(eofbit)(which may throwios_base::failure ([iostate.flags])), and extracts no characters;
- Ifrdbuf()->in_avail() == 0, extracts no characters
- Ifrdbuf()->in_avail() > 0, extractsmin(rdbuf()->in_avail(), n)).
Returns: The number of characters extracted.
basic_istream& 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()callssetstate(failbit)which may throw an exception, and return.
Ifrdbuf()is not null, callsrdbuf()->sputbackc(c).
Ifrdbuf()is null, or ifsputbackcreturnstraits::eof(), callssetstate(badbit)(which may throwios_base::failure ([iostate.flags])).
[Note 2:
This function extracts no characters, so the value returned by the next call togcount()is 0.
— _end note_]
Effects: Behaves as an unformatted input function (as described above), except that the function first clears eofbit.
After constructing a sentry object, if!good()callssetstate(failbit)which may throw an exception, and return.
Ifrdbuf()is not null, callsrdbuf()->sungetc().
Ifrdbuf()is null, or ifsungetcreturnstraits::eof(), callssetstate(badbit)(which may throwios_base::failure ([iostate.flags])).
[Note 3:
This function extracts no characters, so the value returned by the next call togcount()is 0.
— _end note_]
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 togcount().
After constructing a sentry object, ifrdbuf()is a null pointer, returns -1.
Otherwise, callsrdbuf()->pubsync()and, if that function returns -1callssetstate(badbit)(which may throwios_base::failure ([iostate.flags]), and returns-1.
Otherwise, returns zero.
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 togcount().
Returns: After constructing a sentry object, iffail() != false, returnspos_type(-1)to indicate failure.
Otherwise, returnsrdbuf()->pubseekoff(0, cur, in).
basic_istream& 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 togcount().
After constructing a sentry object, iffail() != true, executesrdbuf()->pubseekpos(pos, ios_base::in).
In case of failure, the function callssetstate(failbit)(which may throwios_base::failure).
basic_istream& 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, iffail() != true, executesrdbuf()->pubseekoff(off, dir, ios_base::in).
In case of failure, the function calls setstate(failbit) (which may throwios_base::failure).