Share via


basic_streambuf::sungetc

Gets a character from the stream.

int_type sungetc( );

Return Value

Returns either the character or failure.

Remarks

If a putback position is available, the member function decrements the next pointer for the input buffer and returns traits_type::to_int_type(*gptr). However, it is not always possible to determine the last character read so that it can be captured in the state of the current buffer. If this is true, then the function returns pbackfail. To avoid this situation, keep track of the character to put back and call sputbackc(ch), which will not fail provided you don't call it at the beginning of the stream and you don't try to put back more than one character.

Example

// basic_streambuf_sungetc.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>

int main( ) 
{
   using namespace std;

   ifstream myfile( "basic_streambuf_sungetc.txt", ios::in );

   // Read and increment
   int i = myfile.rdbuf( )->sbumpc( );
   cout << ( char )i << endl;

   // Read and increment
   i = myfile.rdbuf( )->sbumpc( );
   cout << ( char )i << endl;

   // Decrement, read, and do not increment
   i = myfile.rdbuf( )->sungetc( );
   cout << ( char )i << endl;

   i = myfile.rdbuf( )->sungetc( ); 
   cout << ( char )i << endl;

   i = myfile.rdbuf( )->sbumpc( );
   cout << ( char )i << endl;
}

Input: basic_streambuf_sungetc.txt

testing

Output

t
e
e
t
t

Requirements

Header: <streambuf>

Namespace: std

See Also

Reference

basic_streambuf Class

iostream Programming

iostreams Conventions