Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
ciso646 (iso646.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdio (stdio.h)
functions:
clearerr
fclose
feof
ferror
fflush
fgetc
fgetpos
fgets
fopen
fprintf
fputc
fputs
fread
freopen
fscanf
fseek
fsetpos
ftell
fwrite
getc
getchar
gets
perror
printf
putc
putchar
puts
remove
rename
rewind
scanf
setbuf
setvbuf
sprintf
sscanf
tmpfile
tmpnam
ungetc
vfprintf
vprintf
vsprintf
macro constants:
EOF
FILENAME_MAX
NULL
TMP_MAX
objects:
stderr
stdin
stdout
types:
FILE
fpos_t
size_t


ungetc

function
<cstdio>
int ungetc ( int character, FILE * stream );

Unget character from stream

A character is virtually put back into an input stream at the same position the last character was read and the internal file position indicator is decreased back to that previous position so that this character is returned by the next call to a reading operation on that stream.
This character may or may not be the same character as the one last read from the stream in a previous operation. In both cases the value retrieved by the next reading operation on the stream is the one ungot by this function independently of the original character.
Notice though, that this only affects the next reading operation on that character, not the content of the physical file associated with the stream, which is not modified by any calls to this function.
More than one character can be ungot making them available for reading operations in the reverse order they were put back into the stream.
If the End-Of-File internal indicator was set, it is cleared after a call to this function.
A call to fseek, fsetpos or rewind on a stream will discard any characters previously put back into it with this function.
If the argument passed for the character parameter is EOF, the operation fails and the input stream remains unchanged.

Parameters

character
Character to be put back. The character is passed as its int promotion.
stream
Pointer to a FILE object that identifies an input stream.

Return Value

If successful, the character that was pushed back is returned.
On falure, EOF is returned and the stream remains unchanged.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* ungetc example */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  int c;
  char buffer [256];
  pFile = fopen ("myfile.txt","rt");
  if (pFile==NULL) perror ("Error opening file");
  else {
    while (!feof (pFile))
    {
      c=getc (pFile);
      if (c == '#')
        ungetc ('@',pFile);
      else
        ungetc (c,pFile);
      fgets (buffer,255,pFile);
      fputs (buffer,stdout);
    }
  }
  return 0;  
}


This example opens an existing file called myfile.txt for reading and prints its lines, but first gets the first character of every line and puts it back into the stream except if the line begins with #, in which case it is replaced by @.

See also