std::ungetwc(3)
π std::ungetwc
okay bestie, so this one's from the C++ Standard Library β it's std::ungetwc, and no, it's not a weird dance move, it's the "nvm put it back" function for wide characters. let me walk you through it π«Ά
β¨ Synopsis β¨
so first of all, she lives in this header:
#include <cwchar>
and here's her whole signature, literally the entire thing:
std::wint_t ungetwc( std::wint_t ch, std::FILE* stream );
that's it. that's the whole vibe. πΈ
π¦ Description π¦
okay so here's what's going down. if ch does not equal WEOF, she pushes the wide character ch into the input buffer associated with the stream stream β and she does it in such a manner that a subsequent read operation from stream will retrieve that wide character. π
and the actual external device associated with the stream? not modified. not touched. not even a little bit. she's just messing with the buffer, king. π
stream repositioning operations std::fseek, std::fsetpos, and std::rewind all discard the effects of std::ungetwc. so like, yeah, they'll undo your cute little pushback. rude but okay. π
now here's the part where you gotta pay attention, bestie β if std::ungetwc is called more than once without an intervening read or repositioning, it may fail. read that again. in other words, a pushback buffer of size 1 is guaranteed, but any larger buffer is implementation-defined. so one (1) pushback is iconic and safe, and two or more is giving "cross your fingers" energy. π
if multiple successful std::ungetwc were performed, read operations retrieve the pushed-back wide characters in reverse order of std::ungetwc. last one back is first one out β LIFO realness, no cap. π₯
if ch equals WEOF, the operation fails and the stream is not affected. like she simply didn't show up. π
a successful call to std::ungetwc clears the end of file status flag feof. so if you were at EOF and you push something back, bestie you are no longer at EOF. character development. π
and finally β a successful call to std::ungetwc on a stream (whether text or binary) modifies the stream position indicator in an unspecified manner, but guarantees that after all pushed-back wide characters are retrieved with a read operation, the stream position indicator is equal to its value before std::ungetwc. translation: she's allowed to be messy while she's working, but she will put it back exactly where she found it. periodt. β¨
πΈ Parameters πΈ
chβ the wide character to be put back. the little guy going back into the buffer πstreamβ the file stream to put the wide character back to. the destination, the home, the vibe π
π Return value π
- On success,
chis returned. she ate and she came back. π - On failure,
WEOFis returned β and the given stream remains unchanged. so if it flops, at least it flops cleanly. π§
β See also β
ungetcβ puts a character back into a file stream (function). the narrow-char cousin, same energy πfgetwcβ gets a wide character from a file stream (function). the one that picks the character back up lolgetwcβ (function). another way to pull a wide character out π¦- C documentation for
ungetwcβ if you want to see where she came from π
so yeah, that's std::ungetwc. push one (1) wide character back, don't get greedy, remember that repositioning wipes the slate, and she'll always restore the position indicator once you read everything out. slay. π
β¨
mwah, this page was served up fresh from http://cppreference.com, dated 2024.06.10 β go be great with your wide characters, 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