streambuf::sputbackc
int sputbackc ( char c );
streambuf
  cplusplus.com  

Put character back.
  The function checks if c matches the character right before the current position of the input sequence, if so, the get pointer is moved back one character, making the character once again available to input functions.

Parameters.

c
Character to be put back. This must match the character previusly got.

Return Value.
  The value of the character put back.
  If parameter c doesn't match the expected character or if the get pointer is at the beginning of the input sequence, the value returned is EOF.

Example.

// sputbackc
#include <iostream>
using namespace std;

int main () {

  char ch;
  long n;
  streambuf * pbuf;

  pbuf = cin.rdbuf();

  cout << "Please enter some letters and after a number: ";

  do {
    ch=pbuf->sbumpc();

    if ( (ch>='0') && (ch <='9') )
    {
      pbuf->sputbackc (ch);
      cin >> n;
      cout << "You entered number " << n << endl;
      break;
    }
  } while (ch != EOF);

  return 0;
}
This example gets characters form standard input one by one. When the first numeric digit is found, sputback is called to restore the position in the stream to that digit in order to be extracted as part of a number using standard operator >>.

Basic template member declaration ( basic_streambuf<charT,traits> ):
typedef traits::char_type char_type;
typedef traits::int_type int_type;
int_type sputbackc ( char_type c );

See also.
  sungetc, sgetc, sbumpc, sputc
  streambuf class


© The C++ Resources Network, 2001