ungetc、ungetwcungetc, ungetwc

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

语法Syntax

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

参数Parameters

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

stream
指向文件结构的指针。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.

备注Remarks

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

要求Requirements

例程所返回的值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.

示例Example

// 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