C++ streambuf - sungetc


Advertisements

Description

It is used to decrease current position and attempts to move the current position indicator of the controlled input sequence back one position to the character that precedes the current one, making the character at that position available once again for the next input operation.

Declaration

Following is the declaration for std::streambuf::sungetc.

int sungetc();

Parameters

none

Return Value

It returns the value of the new current character of the controlled input sequence, as a value of type int.

Exceptions

Basic guarantee − if an exception is thrown, the stream buffer is in a valid state.

Data races

It modifies the stream buffer object.

Example

In below example explains about std::streambuf::sungetc.

#include <iostream>     
#include <cstdio>       

int main () {
   char ch;
   std::streambuf * pbuf = std::cin.rdbuf();

   std::cout << "Please, enter some letters and then a number: ";
   do {
      ch = pbuf->sbumpc();

      if ( (ch>='0') && (ch <='9') ) {
         pbuf->sungetc ();
         long n;
         std::cin >> n;
         std::cout << "You entered number " << n << '\n';
         break;
      }
   } while ( ch != EOF );

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Please, enter some letters and then a number:
streambuf.htm
Advertisements