by Dinesh Thakur Category: File Handling

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");
   if( fptr ==NULL)
       printf("File could not be opened");
       exit (1);
        printf("File is open for reading.\n");
        while ((ch= fgetc(fptr))!= EOF) //read character by character
              ungetc(ch, fptr); //if ch =='n' push it back
              printf("%c",ch);  // display the character
              fclose (fptr);
              printf ("\n");

Function ungetc() in C

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.