重設資料流的錯誤指標。 此函式有更安全的版本可供使用;請參閱 clearerr_s。
語法
void clearerr(
FILE *stream
);
參數
stream
FILE 結構的指標。
備註
clearerr 函式會重設 stream 的錯誤指標和檔案結尾指標。 不會自動清除錯誤指標;設定指定數據流的錯誤指標之後,該數據流上的作業會繼續傳回錯誤值,直到clearerr呼叫、 fseekfsetpos或 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