public member function
<istream> <iostream>

std::basic_istream::putback

basic_istream& putback (char_type c);
Put character back
Attempts to decrease the current location in the stream by one character, making the last character extracted from the stream once again available to be extracted by input operations.

Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it calls sputbackc(c) on its associated stream buffer object (if any). Finally, it destroys the sentry object before returning.

If the eofbit flag is set before the call, the function fails (sets failbit and returns).
The function clears the eofbit flag, if set before the call.

If the call to sputbackc fails, the function sets the badbit flag. Note that this may happen even if c was indeed the last character extracted from the stream (depending on the internals of the associated stream buffer object).

Calling this function sets the value returned by gcount to zero.

Parameters

c
Character to be put back.
If this does not match the character at the put back position, the behavior depends on the particular stream buffer object associated to the stream:
  • In string buffers, the value is overwritten for output stream buffers, but the function fails on input buffers.
Other types of stream buffer may either fail, be ignored, or overwrite the character at that position.
Member type char_type is the type of characters used by the stream (i.e., its first class template parameter, charT).

Return Value

The basic_istream object (*this).

Errors are signaled by modifying the internal state flags:
flagerror
eofbit-
failbitThe construction of sentry failed (such as when the stream state was not good before the call).
badbitEither the internal call to sputbackc failed, or another error occurred on the stream (such as when the function catches an exception thrown by an internal operation, or when no stream buffer is associated with the stream).
When set, the integrity of the stream may have been affected.
Multiple flags may be set by a single operation.

If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// istream::putback example
#include <iostream>     // std::cin, std::cout
#include <string>       // std::string

int main () {
  std::cout << "Please, enter a number or a word: ";
  char c = std::cin.get();

  if ( (c >= '0') && (c <= '9') )
  {
    int n;
    std::cin.putback (c);
    std::cin >> n;
    std::cout << "You entered a number: " << n << '\n';
  }
  else
  {
    std::string str;
    std::cin.putback (c);
    getline (std::cin,str);
    std::cout << "You entered a word: " << str << '\n';
  }
  return 0;
}

Possible output:
Please, enter a number or a word: pocket
You entered a word: pocket


Data races

Modifies the stream object.
Concurrent access to the same stream object may cause data races.

Exception safety

Basic guarantee: if an exception is thrown, the object is in a valid state.
It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.

See also