Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_streambuf<CharT,Traits>::sungetc

      From cppreference.com
      <cpp‎ |io‎ |basic streambuf
       
       
       
      std::basic_streambuf
       
      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 callspbackfail() to back up the input sequence if possible.

      The I/O stream functionbasic_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 toint_type withTraits::to_int_type(*gptr()). The next single-character input from this streambuf will return this character.

      If putback position was not available, returns whatpbackfail() returns, which isTraits::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' againchar 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() failsif(eof==EOF)std::cout<<"Nothing to unget after 'a'\n";}

      Output:

      aaabNothing to unget after 'a'

      [edit]See also

      puts one character back in the input sequence
      (public member function)[edit]
      unextracts a character
      (public member function ofstd::basic_istream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_streambuf/sungetc&oldid=116865"

      [8]ページ先頭

      ©2009-2025 Movatter.jp