Places a character back into a stream.
#include <stdio.h> int ungetc(int c, FILE *stream);
c
The character to return to a stream.
stream
A pointer to a file stream.
The ungetc() function places character c back into stream's buffer. The next read operation will read the character placed by ungetc(). Only one character can be pushed back into a buffer until a read operation is performed.
The function's effect is ignored when an fseek(), fsetpos(), or rewind() operation is performed.
ungetc() returns c if it is successful and returns EOF if it fails.
This facility may not be available on configurations of the EWL that run on platforms without file systems.
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *f; int c; // create a new file for output and input if ( (f = fopen("double.txt", "w+")) == NULL) { printf("Can't open.\n"); exit(1); } // output text to the file fprintf(f, "The quick brown fox\n"); fprintf(f, "jumped over the moon.\n"); // move the file position indicator // to the beginning of the file rewind(f); printf("Reading each character twice.\n"); // read a character while ( (c = fgetc(f)) != EOF) { putchar(c); // Put the character back into the stream ungetc(c, f); c = fgetc(f);// read the same character again putchar(c); } fclose(f); return 0; } Output Reading each character twice. TThhee qquuiicckk bbrroowwnn ffooxx jjuummppeedd oovveerr tthhee mmoooonn..