std::basic_...buf::sungetc(3)
hey bestie π youβve landed on the std::basic_...buf::sungetc(3) page from the C++ Standard Libary manual (yes, thatβs the headerβs spelling, no cap π). this is the C++ Standard Library function std::basic_streambuf::sungetc, and sheβs here to put one character back like itβs nothing. periodt. π
β¨ NAME β¨
std::basic_streambuf::sungetc β yep, thatβs her. the member function that handles the one-character putback situation. iconic behavior only. π
πΈ Synopsis πΈ
int_type sungetc();
okay so hereβs the actual tea β: if a putback position is available in the get area β and the vibe check is gptr() > eback() β then it decrements the next pointer (gptr()) and returns the character it now points to. slay. β¨
if a putback position is not available, then it calls pbackfail() to back up the input sequence if possible. fingers crossed, bestie π«Ά
and fun fact: the I/O stream function basic_istream::unget is implemented in terms of this function. so yeah, sheβs the blueprint. π
π Parameters π
(none) β literally no parameters. not a single argument. sheβs low-maintenance like that. π
π₯ Return value π₯
- if a putback position was available: returns the character that the next pointer is now pointing at, converted to
int_typewithTraits::to_int_type(*gptr()). the next single-character input from this streambuf will return this character. so you literally get that same char next time. iconic. π - if a putback position was not available: returns what
pbackfail()returns, which isTraits::eof()on failure. sad girl hours if that happens π.
π¦ 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'
π See also π
sputbackcβ puts one character back in the input sequence (public member function).ungetβ unextracts a character (public member function ofstd::basic_istream<CharT,Traits>).
mwah π source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::basic_...buf::sungetc(3). now go unget responsibly, bestie ππβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms