Node:How Unread, Previous:Unreading Idea, Up:Unreading
ungetc
To Do UnreadingThe function to unread a character is called ungetc
, because it
reverses the action of getc
.
int ungetc (int c, FILE *stream) | Function |
The ungetc function pushes back the character c onto the
input stream stream. So the next input from stream will
read c before anything else.
If c is The character that you push back doesn't have to be the same as the last
character that was actually read from the stream. In fact, it isn't
necessary to actually read any characters from the stream before
unreading them with The GNU C library only supports one character of pushback--in other
words, it does not work to call Pushing back characters doesn't alter the file; only the internal
buffering for the stream is affected. If a file positioning function
(such as Unreading a character on a stream that is at end of file clears the end-of-file indicator for the stream, because it makes the character of input available. After you read that character, trying to read again will encounter end of file. |
wint_t ungetwc (wint_t wc, FILE *stream) | Function |
The ungetwc function behaves just like ungetc just that it
pushes back a wide character.
|
Here is an example showing the use of getc
and ungetc
to
skip over whitespace characters. When this function reaches a
non-whitespace character, it unreads that character to be seen again on
the next read operation on the stream.
#include <stdio.h> #include <ctype.h> void skip_whitespace (FILE *stream) { int c; do /* No need to check forEOF
because it is notisspace
, andungetc
ignoresEOF
. */ c = getc (stream); while (isspace (c)); ungetc (c, stream); }