std::ungetwc(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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, ch is returned. she ate and she came back. πŸ’…
  • On failure, WEOF is 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 lol
  • getwc β€” (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