ungetc, ungetwc

推后字符在流中。

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

参数

  • c
    将驱动器的字符。

  • stream
    为 FILE 结构的指针。

返回值

如果成功,这些函数都返回一个字符参数 c*。*如果 c 不能推回,或者字符未读取输入流,未更改,并 ungetc 返回 EOF; ungetwc 返回 WEOF。 如果 stream 是 NULL,无效参数调用处理程序,如 参数验证所述。 如果执行允许继续,EOF 或 WEOF 返回,并且 errno 设置为 EINVAL。

有关这些属性和其他错误代码的信息,请参见 _doserrno、errno、_sys_errlist 和_sys_nerr

备注

ungetc 功能推后字符 c 在 stream 上并清除文件结尾指示符。 流必须处于打开状态用于读取。 在 stream 的一个后续读取操作从 c启动*。*尝试推送到流中的 EOF 使用 ungetc 被忽略。

可以清除在流中的字符由 ungetc,如果 fflush、fseek、fsetpos或 rewind 调用,在字符从流之前读取。 它的文件位置指示符将具有值,在字符。推回之前。 与流相应的外部存储保持不变。 在成功的 ungetc 调用文本流,文件位置指示符为未指定的,直到所有已推回的字符读取或被丢弃。 在每次成功的 ungetc 调用二进制流,文件位置指示符递减;如果它的值是 0,所以调用;值在调用后之前未定义。

结果是不可预知的,如果 ungetc 两次调用,没有读取或这两者之间的文件确定的操作调用。 在对 fscanf的调用中,为 ungetc 的调用可能失败后,除非另一个读取操作 (例如 getc) 执行了。 这是因为,fscanf 调用 ungetc。

ungetwc 是 ungetc的宽字符版本。 但是,在每次成功的 ungetwc 调用文本或二进制流,文件位置指示符的值是未指定的,直到所有已推回的字符读取或被丢弃。

这些功能是线程安全和锁定敏感数据在执行时。 有关非固定版本,请参见 _ungetc_nolock, _ungetwc_nolock

一般文本例程映射

TCHAR.H 实例

未定义的_UNICODE & _MBCS

定义的_MBCS

定义的_UNICODE

_ungettc

ungetc

ungetc

ungetwc

要求

实例

必需的标头

ungetc

<stdio.h>

ungetwc

<stdio.h> 或 <wchar.h>

控件个在 Windows 应用商店 apps 不受支持。 标准流处理与控件个,stdin,stdout和 stderr,在 C 运行时函数在 Windows 应用商店 apps 之前,可以使用它们必须重定向。 有关其他的兼容性信息,请参见中介绍的 兼容性

示例

// 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() );
}
  

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见 平台调用示例

请参见

参考

流I/O

getc, getwc

putc, putwc