ungetc [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Library Reference Manual
ungetc
Pushes back a single character onto an open stream.
Syntax
#include <stdio.h>
int ungetc (int c, FILE *stream);
Parameters
c A single character to push back.
stream A pointer to an open stream.
Return Values
x The value of the argument c, indicating success.
EOF An error occurred.
Description
The ungetc function pushes the character specified by c (converted to an
unsigned char) back onto the input stream pointed to by stream. The
pushed-back character is returned by subsequent reads on the stream in
the reverse order of their pushing. A successful intervening call to
stream to a file positioning function (fseek(), fsetpos(), or rewind())
discards any pushed-back characters for the stream. The external storage
corresponding to the stream is unchanged.
One character pushback is guaranteed. If the ungetc() function is called
too many times on the same stream without an intervening read or file
positioning operation on that stream, the operation may fail.
If the value of c equals that of the macro EOF, the operation fails and
the input stream is unchanged.
A successful call to ungetc() clears the end-of-file indicator for the
stream. The value of the file position indicator for the stream after
reading or discarding all pushed-back characters is the same as it was
before the characters were pushed back.
For a text stream, the value of its file position indicator after a
successful call to ungetc() is unspecified until all pushed-back
characters are read or discarded. For a binary stream, its file position
indicator is decremented by each successful call to ungetc(), if its
value was zero before the call, it is indeterminate after the call.
Examples
The following program reads one character from stdin, pushes it back onto
stdin, rereads the character, and checks to make sure that this character
and the character originally pushed back are the same. A message is
printed on stdout stating the outcome of the comparison.
#include <stdio.h>
main()
{
int c1, c2;
c1 = getchar();
ungetc(c1, stdin);
c2 = getchar();
if(c1 == c2)
printf("They're the same!\n");
else
printf("Oops! They're different!\n");
}
At least one character may be pushed back if data has been read from the
stream prior to the push-back attempt and if the stream is buffered.
See Also
fseek(), fsetpos(), ftell(), fclose(), ferror(), fopen(), fread(),
fgetc(), gets(), putc(), fputc(), scanf(), fscanf(), ANSI C 4.9.7.11,
POSIX.1 8.1
MPE/iX 5.0 Documentation