ungetc, ungetwc (Windows CE 5.0)

Send Feedback

Developing an Application > Microsoft C Run-time Library for Windows CE > Run-time Library Reference

Pushes a character back onto the stream.

int ungetc( int c, FILE *stream );wint_t ungetwc( wint_t c, FILE *stream );

Parameters

  • c
    Character to be pushed.
  • stream
    Pointer to FILE structure.

Return Values

If successful, each of these functions returns the character argument c. If c cannot be pushed back or if no character has been read, the input stream is unchanged and ungetc returns EOF; ungetwc returns WEOF.

Remarks

These functions are supported by all versions of the C run-time libraries.

The ungetc function pushes the character c back onto stream and clears the end-of-file indicator. The stream must be open for reading. A subsequent read operation on stream starts with c. An attempt to push EOF onto the stream using ungetc is ignored.

Characters placed on the stream by ungetc may be erased if fflush, fseek, or fsetpos is called before the character is read from the stream. The file-position indicator will have the value it had before the characters were pushed back. The external storage corresponding to the stream is unchanged.

On a successful ungetc call against a text stream, the file-position indicator is unspecified until all the pushed-back characters are read or discarded. On each successful ungetc call against a binary stream, the file-position indicator is decremented; if its value was 0 before a call, the value is undefined after the call.

Results are unpredictable if ungetc is called twice without a read or file-positioning operation between the two calls. After a call to fscanf, a call to ungetc may fail unless another read operation has been performed. This is because fscanf itself calls ungetc.

ungetwc is a wide-character version of ungetc. However, on each successful ungetwc call against a text or binary stream, the value of the file-position indicator is unspecified until all pushed-back characters are read or discarded.

The following table shows generic-text routine mappings for this function.

TCHAR.H Routine _UNICODE Defined
_ungettc ungetwc

For more information about TCHAR.H routines, see Generic Text Mappings.

Example

Description

This program first converts a character representation of an unsigned integer to an integer. If the program encounters a character that is not a digit, the program uses ungetc to replace it in the stream.

Code

#include <stdio.h>
#include <ctype.h>

void main( void )
{
   int ch;
   int result = 0;

   printf( "Enter an integer: " );

   /* Read in and convert number: */
   while( ((ch = getchar()) != EOF) && isdigit( ch ) )
      result = result * 10 + ch - '0';    /* Use digit. */
   if( ch != EOF )
      ungetc( ch, stdin );                /* Put nondigit back. */
   printf( "Number = %d\nNextcharacter in stream = '%c'", 
            result, getchar() );
}
// Output
Enter an integer: 521a
Number = 521
Nextcharacter in stream = 'a'

Requirements

OS Versions: Windows CE 2.0 and later.

Header: ctype.h, stdlib.h, stdio.h.

Link Library: coredll.dll.

See Also

fflush | fputc | fseek | fsetpos | fscanf

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.