< cpp‎ | io‎ | basic streambuf
Revision as of 07:09, 9 December 2012 by Cubbi (Talk | contribs)

int_type sputbackc(char_type c);

If a putback position is available in the get area (gptr() > eback()), and the character c is equal to the character one position to the left of Template:rlpf (as determined by Traits::eq(c, gptr()[-1]), then simply decrements the next pointer (Template:rlpf).

Otherwise, calls pbackfail(Traits::to_int_type(c)) to either back up the get area or to modify both the get area and possibly the associated character sequence.

The I/O stream function basic_istream::putback is implemented in terms of this function.




Return value

If putback position was available, returns the character that the next pointer is now pointing at, converted to int_type with Traits::to_int_type(*gptr()). The next single-character input from this streambuf will return this character.

If putback position was not available, returns what Template:rlpf returns, which is Traits::eof() on failure.


#include <iostream>
#include <sstream>
int main()
    std::stringstream s("abcdef"); // gptr() poitns to 'a'
    std::cout << "Before putback, string holds " << s.str() << '\n';
    char c1 = s.get(); // c = 'a', gptr() now points to 'b' 
    char c2 = s.rdbuf()->sputbackc('z'); // same as s.putback('c')
                                         // gptr() now points to 'z'
    std::cout << "After putback, string holds " << s.str() << '\n';
    char c3 = s.get(); // c3 = 'z', gptr() now points to 'b'
    std::cout << c1 << c2 << c3 << '\n';
    s.rdbuf()->sputbackc('b');  // back to 'b'
    s.rdbuf()->sputbackc('z');  // back to 'z'
    int eof = s.rdbuf()->sputbackc('x');  // nothing to unget: pbackfail() fails
    if(eof == EOF)
        std::cout << "No room to putback after 'z'\n";


Before putback, string holds abcdef
After putback, string holds zbcdef
No room to putback after 'z'

See also

Template:cpp/io/basic streambuf/dcl list sungetcTemplate:cpp/io/basic istream/dcl list putback