std::basic_...ambuf::sgetc(3)

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

πŸ’– NAME πŸ’–

std::basic_streambuf::sgetc β€” ok so literally the whole name is just std::basic_streambuf::sgetc, bestie, that's the vibe. Also hi bestie, welcome to the C++ Standard Library corner of the manual, section 3, we're doing streambuf peek-y stuff today 🫢


✨ SYNOPSIS ✨

int_type sgetc();

Reads one character from the input sequence. Yes, just one. She's a one-character girlie and she's not sorry πŸ’…

Here's the little plot twist you actually need to know: if the input sequence read position is not available, this bad girl returns underflow(). Otherwise? She returns Traits::to_int_type(*gptr()). No cap. That's the whole contract ✨


🌸 PARAMETERS 🌸

None. Nothing. Zero. Zilch. (none). The way she takes no arguments at all, iconic behaviour honestly πŸŽ€


πŸ¦‹ RETURN VALUE πŸ¦‹

The value of the character pointed to by the get pointer. That's it, that's the tea πŸ“


πŸŽ€ EXAMPLE πŸŽ€

ok so peep this, it's giving peek() energy but make it streambuf:

// Run this code

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream stream("Hello, world");
    std::cout << "sgetc() returned '" << (char)stream.rdbuf()->sgetc() << "'\n";
    std::cout << "peek() returned '" << (char)stream.peek() << "'\n";
    std::cout << "get() returned '" << (char)stream.get() << "'\n";
}

And the output, crown on:

sgetc() returned 'H'
peek() returned 'H'
get() returned 'H'

not all three of them returning 'H' 😭 they all looked at her and none of them moved the cursor, she's literally still sitting there. main character energy fr fr πŸ”₯


πŸ’œ SEE ALSO πŸ’œ

  • sbumpc β€” reads one character from the input sequence and advances the sequence (public member function). She actually moves. Unlike some people 😌
  • stossc (removed in C++17) β€” advances the input sequence, then reads one character without advancing again (public member function). Girl got deleted from the standard, pour one out πŸ’€
  • snextc β€” advances the input sequence, then reads one character without advancing again (public member function)
ngl if you're mixing these up, just remember: sgetc peeks, sbumpc commits, snextc/stossc do the shuffle first. vibe check passed 🌈

And that's the page, bestie! Big shoutout to http://cppreference.com, version 2024.06.10 β€” she's sourced, she's dated, she's citable, periodt πŸ‘‘βœ¨ Go read your streams and stay hydrated 🧚

πŸ“¦ where this page comes from