Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
IOstream Library
manipulators
classes:
filebuf
fstream
ifstream
ios
iostream
ios_base
istream
istringstream
ofstream
ostream
ostringstream
streambuf
stringbuf
stringstream
objects:
cerr
cin
clog
cout
types:
fpos
streamoff
streampos
streamsize
streambuf
streambuf::streambuf
streambuf::~streambuf
public members:
streambuf::getloc
streambuf::in_avail
streambuf::pubimbue
streambuf::pubseekoff
streambuf::pubseekpos
streambuf::pubsetbuf
streambuf::pubsync
streambuf::sbumpc
streambuf::sgetc
streambuf::sgetn
streambuf::snextc
streambuf::sputbackc
streambuf::sputc
streambuf::sputn
streambuf::sungetc
protected members:
streambuf::eback
streambuf::egptr
streambuf::epptr
streambuf::gbump
streambuf::gptr
streambuf::pbase
streambuf::pbump
streambuf::pptr
streambuf::setg
streambuf::setp
virtual prot. members:
streambuf::imbue
streambuf::overflow
streambuf::pbackfail
streambuf::seekoff
streambuf::seekpos
streambuf::setbuf
streambuf::showmanyc
streambuf::sync
streambuf::uflow
streambuf::underflow
streambuf::xsgetn
streambuf::xsputn


streambuf::sputbackc

public member function
int sputbackc ( char c );

Put character back

The get pointer is moved back to point to the character right before its current position so the last character gotten, c, becomes available again as the character to be read at that position by the next input operation.

During its operation, the function calls the protected virtual member function pbackfail either if the character c doesn't match gptr()[-1] or if the get pointer gptr points to the same position as the beginning pointer eback.

When c does not match the character at that position, the default definition of pbackfail in streambuf will prepend c to be the character extracted at that position if possible, but derived classes may override this behavior.

The member function sungetc behaves in a similar way but without taking any parameters.

Parameters

c
Character to be put back.

Return Value

The value of the character put back.
If the get pointer is at the beginning of the input sequence or any other error occurs, the value returned is EOF (or traits::eof() for other traits).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// sputbackc
#include <iostream>
using namespace std;
int main () {
  char ch;
  long n;
  streambuf * pbuf;
  pbuf = cin.rdbuf();
  cout << "Please enter some letters and then 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 the 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 the extraction operator >>.

Basic template member declaration

( basic_streambuf<charT,traits> )
1
2
3
typedef traits::char_type char_type;
typedef traits::int_type int_type;
int_type sputbackc ( char_type c );


See also