C examples for stdio.h:ungetc
function
<cstdio> <stdio.h>
Unget character from stream, Undo previous getc operation.
int ungetc ( int character, FILE * stream );
Parameter | Description |
---|---|
character | The character to be put back. |
stream | a FILE object. |
On success, the character put back is returned.
If the operation fails, EOF is returned.
#include <stdio.h> int main ()/*from w w w .j a v a 2 s . co m*/ { FILE * pFile; int c; char buffer [256]; pFile = fopen ("main.cpp","rt"); if (pFile==NULL) { perror ("Error opening file"); return -2; } while (!feof (pFile)) { c=getc (pFile); if (c == EOF) break; if (c == '#') ungetc ('@',pFile); else ungetc (c,pFile); if (fgets (buffer,255,pFile) != NULL) fputs (buffer,stdout); else break; } return 0; }