Mega Code Archive

 
Categories / C / Code Snippets
 

Put a character back to the input stream

//Declaration: int ungetc(int ch, FILE *stream); //Return: returns ch on success or EOF on failure. #include <stdio.h> int main () { FILE * filep; int c; char buffer [256]; filep = fopen ("testfile.txt","rt"); if (filep==NULL) perror ("Error opening file"); else { while (!feof (filep)) { c=getc (filep); if (c == '#') ungetc ('@',filep); else ungetc (c,filep); fgets (buffer,255,filep); fputs (buffer,stdout); } } return 0; }