重置流的错误指示符。 此函数有一个更安全的版本;请参阅 clearerr_s。
语法
void clearerr(
FILE *stream
);
参数
stream
指向 FILE 结构的指针。
备注
clearerr 函数为 stream 重置错误指示符和文件尾指示符。 错误指示符不会自动清除;设置指定流的错误指示符后,在调用 clearerr、fseek、fsetpos 或 rewind 前,将继续在该流上执行操作以返回错误值。
如果 stream 为 NULL,则会调用无效的参数处理程序,如参数验证中所述。 如果允许执行继续,则该函数将 errno 设置为 EINVAL 并返回。 有关 errno 和错误代码的详细信息,请参阅 errno 常数。
此函数有一个更安全的版本;请参阅 clearerr_s。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
要求
| 例程 | 必需的标头 |
|---|---|
clearerr |
<stdio.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// crt_clearerr.c
// This program creates an error
// on the standard input stream, then clears
// it so that future reads won't fail.
#include <stdio.h>
int main( void )
{
int c;
// Create an error by writing to standard input.
putc( 'c', stdin );
if( ferror( stdin ) )
{
perror( "Write error" );
clearerr( stdin );
}
// See if read causes an error.
printf( "Will input cause an error? " );
c = getc( stdin );
if( ferror( stdin ) )
{
perror( "Read error" );
clearerr( stdin );
}
else
printf( "No read error\n" );
}
输入
n
输出
Write error: No error
Will input cause an error? n
No read error