| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Global objects | ||||
| Member functions | ||||
(C++11) | ||||
| Formatted input | ||||
| Unformatted input | ||||
basic_istream::putback | ||||
| Positioning | ||||
| Miscellaneous | ||||
(C++11) | ||||
| Member classes | ||||
| Non-member functions | ||||
basic_istream& putback( char_type ch); | ||
Puts the characterch back to the input stream so the next extracted character will bech.
First clearseofbit, then behaves asUnformattedInputFunction. After constructing and checking the sentry object, ifrdbuf() is not null, callsrdbuf()->sputbackc(ch), which callsrdbuf()->pbackfail(ch) ifch does not equal the most recently extracted character.
Ifrdbuf() is null or ifrdbuf->sputbackc(ch) returnsTraits::eof(), callssetstate(badbit).
In any case, sets thegcount() counter to zero.
Contents |
| ch | - | character to put into input stream |
*this
If an internal operation throws an exception, it is caught andbadbit is set. Ifexceptions() is set forbadbit, the exception is rethrown.
Demonstrates the difference between modifying and non-modifyingputback().
#include <iostream>#include <sstream> int main(){std::stringstream s1("Hello, world");// IO stream s1.get();if(s1.putback('Y'))// modifies the bufferstd::cout<< s1.rdbuf()<<'\n';elsestd::cout<<"putback failed\n"; std::cout<<"--\n"; std::istringstream s2("Hello, world");// input-only stream s2.get();if(s2.putback('Y'))// cannot modify input-only bufferstd::cout<< s2.rdbuf()<<'\n';elsestd::cout<<"putback failed\n"; s2.clear(); std::cout<<"--\n"; if(s2.putback('H'))// non-modifying putbackstd::cout<< s2.rdbuf()<<'\n';elsestd::cout<<"putback failed\n";}
Output:
Yello, world--putback failed--Hello, world
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2243 | C++98 | sputbackc() was called without any argument | called withch |
| puts one character back in the input sequence (public member function of std::basic_streambuf<CharT,Traits>)[edit] | |
| unextracts a character (public member function)[edit] | |
| reads the next character without extracting it (public member function)[edit] |