std::basic_istream<CharT,Traits>::putback

Puts the character ch back to the input stream so the next extracted character will be ch.

# Declarations

basic_istream& putback( char_type ch );

# Parameters

# Return value

*this

# Example

#include <iostream>
#include <sstream>
 
int main()
{
    std::stringstream s1("Hello, world"); // IO stream
    s1.get();
    if (s1.putback('Y')) // modifies the buffer
        std::cout << s1.rdbuf() << '\n';
    else
        std::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 buffer
        std::cout << s2.rdbuf() << '\n';
    else
        std::cout << "putback failed\n"; 
    s2.clear();
 
    std::cout << "--\n";
 
    if (s2.putback('H')) // non-modifying putback
        std::cout << s2.rdbuf() << '\n';
    else
        std::cout << "putback failed\n";
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2243C++98sputbackc() was called without any argumentcalled with ch

# See also