std::basic_streambuf<CharT,Traits>::sungetc
From cppreference.com
< cpp | io | basic streambuf
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
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)
std::basic_streambuf
Public member functions
Locales
Positioning
Get area
Put area
Putback
basic_streambuf::sungetc
Protected member functions
(C++11)
(C++11)
Locales
Positioning
Get area
Put area
Putback
int_type sungetc();
If a putback position is available in the get area (gptr() > eback()), then decrements the next pointer (gptr() ) and returns the character it now points to.
If a putback position is not available, then calls pbackfail() to back up the input sequence if possible.
The I/O stream function basic_istream::unget is implemented in terms of this function.
Contents
[edit] Parameters
(none)
[edit] 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 pbackfail() returns, which is Traits::eof() on failure.
[edit] Example
Run this code
#include <iostream> #include <sstream> int main() { std::stringstream s("abcdef"); // gptr() points to 'a' char c1 = s.get(); // c = 'a', gptr() now points to 'b' char c2 = s.rdbuf()->sungetc(); // same as s.unget(): gptr() points to 'a' again char c3 = s.get(); // c3 = 'a', gptr() now points to 'b' char c4 = s.get(); // c4 = 'b', gptr() now points to 'c' std::cout << c1 << c2 << c3 << c4 << '\n'; s.rdbuf()->sungetc(); // back to 'b' s.rdbuf()->sungetc(); // back to 'a' int eof = s.rdbuf()->sungetc(); // nothing to unget: pbackfail() fails if (eof == EOF ) std::cout << "Nothing to unget after 'a'\n"; }
Output:
aaab Nothing to unget after 'a'