basic_istream::putback

To replace a previously extracted character.

  basic_istream<charT, traits>& putback

     (char_type c);  
Remarks

The function putback() allows you to replace the last character extracted by calling rdbuf()->sungetc(). If the buffer is empty, or if sungetc() returns eof, setstate(failbit) may be called.

Return

The this pointer is returned.

See Also

basic_istream::unget

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

  #include <iostream>
  #include <fstream> 
  #include <stdlib.h>

  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.putback(tmp);
           else continue;
        }                         
        cout << ch;
     }
     return 0;
  }  

Result:

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