Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>ungetc() push character back onto the stream</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 ungetc()                Push Character Back onto the Stream

 #include   <stdio.h>

 int        ungetc(c,stream);
 int        c;                           Character to be pushed
 FILE       *stream;                     Pointer to file structure

    ungetc() pushes 'c' back onto the input stream specified by 'stream'.
    'stream' must be buffered and open for reading.  The next call to
    getc() or fread() after a call to ungetc() returns 'c'.  An attempt
    to push EOF is ignored.

       Returns:     The character argument 'c' is returned, if
                    successful.  EOF indicates a failure to push back 'c'
                    or an attempt to push back a character before any
                    have been read.

         Notes:     If ungetc() is called twice without an intervening
                    call to getc(), the character pushed back by the
                    first ungetc() call is erased.  Likewise, fseek()
                    will erase a character that has been pushed back if
                    it is called before the character is reread.

   -------------------------------- Example ---------------------------------

    The following statements open a file, read characters, and push back
    the last character read.

           #include <stdio.h>

           FILE *stream;
           int i, ch;
           char *buffr;

           main()
           {
               if ((stream = fopen("msg.txt","r+")) != NULL) {
                  i = 0;
                  while ((ch = getc(stream)) != '@')
                        buffr[i++] = ch;
                  ungetc(ch,stream);
                  fclose(stream);
               }
           }


See Also: ungetch() getc() putc()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson