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.

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

返回值Return Value

如果成功,其中每个函数将返回字符自变量cIf successful, each of these functions returns the character argument c. 如果无法推送回 c 或未读取任何字符,则输入流不改变且 ungetc 返回 EOFungetwc 返回 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. 如果 streamNULL,则将调用无效的参数处理程序,如参数验证中所述。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 重新推送到 stream 上,并清除文件结尾指示符。The ungetc function pushes the character c back onto stream and clears the end-of-file indicator. 流必须打开以供读取。The stream must be open for reading. 在进行的后续读取操作stream开头cA subsequent read operation on stream starts with c. 将忽略尝试使用 ungetcEOF 重新推送到流上。An attempt to push EOF onto the stream using ungetc is ignored.

如果从流中读取此字符前调用了 fflushfseekfsetpos、或 rewind,则可能会消除 ungetc 在流上放置的字符。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.

ungetwcungetc 的宽字符版本。ungetwc 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 ungetc ungetc ungetwc


例程所返回的值Routine 必需的标头Required header
ungetc <stdio.h><stdio.h>
ungetwc <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/O Stream I/O
getc、getwc getc, getwc
putc、putwcputc, putwc