The function prototype is written in the following manner:
int ungetc(int c, File *stream);
The function pushes the character specified by int c back into the input stream. The pushed back character is read by subsequent read statement. In Program the function ungetc () is used to push back character ‘n’ during reading of the file created.
Illustrates application of function ungetc ()
#include <stdio.h> #include <stdlib.h> void main() { FILE* fptr; char ch; fptr = fopen ("Hello.txt","r"); clrscr(); if( fptr ==NULL) { printf("File could not be opened"); exit (1); } else { printf("File is open for reading.\n"); while ((ch= fgetc(fptr))!= EOF) //read character by character if(ch=='n') { ungetc(ch, fptr); //if ch =='n' push it back printf("%c",ch); // display the character fclose (fptr); } printf ("\n"); } }