basic_istream::unget

To replace a previously extracted character.

basic_istream<charT, traits>&unget();

Remarks

Use the function unget() to return the previously extracted character. If rdbuf() is null or if end-of-file is encountered setstate(badbit) is called.

The this pointer is returned.

See Also

basic_istream::putback , basic_istream::ignore

Listing: Example of basic_istream::unget() usage:
// The file ewl-test contains:
// char ch;   // to save char
//             /* comment will remain */ 
//              // read until failure
// while((ch = in.get()) != EOF) cout.put(ch); 

#include <iostream>
#include <fstream>
#include <cstdlib>

char inFile[] = "ewl-test";
char bslash = '/';

int main()
{
using namespace std;
  ifstream in(inFile);
   if(!in.is_open()) 
   {cout << "file not opened"; exit(1);}
   char ch, tmp;
   while((ch = in.get()) != EOF)
   {
   if(ch == bslash)
   {
      in.get(tmp);
      if(tmp != bslash)
         in.unget();
         else continue;
      } 
   cout << ch;
   }
   return 0;
}

Result:

  char ch;   // to save char
              /* comment will remain */ 
              // read until failure
  while((ch = in.get()) != EOF) cout.put(ch);