ungetc、ungetwcungetc, ungetwc

将字符重新推送到流上。Pushes a character back onto the stream.


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


要推送的字符。Character to be pushed.

指向文件结构的指针。Pointer to FILE structure.

返回值Return Value

如果成功,其中每个函数将返回字符自变量cIf successful, each of these functions returns the character argument c. 如果c无法推送回或已不读取任何字符,如果输入的流保持不变并ungetc返回 * * EOF; **ungetwc返回WEOFIf c cannot be pushed back or if no character has been read, the input stream is unchanged and ungetc returns EOF; **ungetwc returns **WEOF. 如果NULL,则调用无效参数处理程序,如中所述参数验证If stream is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则EOFWEOF返回和errno设置为EINVALIf execution is allowed to continue, EOF or WEOF is returned and errno is set to EINVAL.

有关这些代码及其他错误代码的信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrFor information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.


Ungetc函数将字符推送c并清除文件尾指示器。The ungetc function pushes the character c back onto stream and clears the end-of-file indicator. 流必须打开以供读取。The stream must be open for reading. 在进行的后续读取操作开头cA subsequent read operation on stream starts with c. 尝试推送EOF到流使用ungetc将被忽略。An attempt to push EOF onto the stream using ungetc is ignored.

字符放置在由流上ungetc是否可能清除fflushfseekfsetpos,或rewind之前从流读取字符时调用。Characters placed on the stream by ungetc may be erased if fflush, fseek, fsetpos, or rewind 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. 在成功ungetc直至所有读取或丢弃的推送回字符对文本流、 文件位置指示器的调用未指定。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. 在每个成功ungetc针对二进制流,文件位置指示器的调用是递减; 如果其值为 0 调用之前,未定义值调用的后面。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.

结果是不可预知如果ungetc两次未读取或文件定位操作之间的两个调用就调用。Results are unpredictable if ungetc is called twice without a read or file-positioning operation between the two calls. 调用了fscanf,调用ungetc可能失败,除非另一个读取操作 (如getc) 已执行。After a call to fscanf, a call to ungetc may fail unless another read operation (such as getc) has been performed. 这是因为fscanf本身调用ungetcThis is because fscanf itself calls ungetc.

ungetwc是宽字符版本的ungetcungetwc is a wide-character version of ungetc. 但是,在每个成功ungetwc是未指定的文本或二进制流中,文件位置指示器的值对的调用直到所有推送回字符被读取或被丢弃。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.

这些函数线程安全并会在执行期间锁定敏感数据。These functions are thread-safe and lock sensitive data during execution. 有关非锁定版本,请参阅 _ungetc_nolock、_ungetwc_nolockFor a non-locking version, see _ungetc_nolock, _ungetwc_nolock.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_ungettc_ungettc ungetcungetc ungetcungetc ungetwcungetwc


例程Routine 必需的标头Required header
ungetcungetc <stdio.h><stdio.h>
ungetwcungetwc <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 控制台中,与关联的标准流句柄stdinstdout,和stderr,必须将 C 运行时函数才能使用它们在 UWP 应用重定向.The standard stream handles that are associated with the console, stdin, stdout, and stderr, must be redirected before C run-time functions can use them in UWP apps. 有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_ungetc.c
// 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.

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

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

   // 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\nNext character in stream = '%c'",
            result, getchar() );

      521aNumber = 521
Next character in stream = 'a'

请参阅See also

流 I/OStream I/O
getc、getwcgetc, getwc
putc、putwcputc, putwc