Place a character back into a stream.
Compatibility:This function is compatible with the following targets:
|
|
#include <stdio.h>
int ungetc(int c, FILE *stream);Parameters:
Parameters for this facility are:
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.
"Wide Character and Byte Character Stream Orientation"
Example of ungetc() usage.:#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *f;
int c;
// create a new file for output and input
if ( (f = fopen("myfoo", "w+")) == NULL) {
printf("Can't open myfoo.\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;
}