函式
<cstdio>

clearerr

void clearerr ( FILE * stream );
清除錯誤指示符
重置流 (stream)錯誤 (error)檔案尾 (eof) 指示符。

當 I/O 函式因錯誤或到達檔案末尾而失敗時,流 (stream) 的這兩個內部指示符之一可能會被設定。透過呼叫此函式,或呼叫以下任一函式,可以清除這些指示符的狀態:rewindfseekfsetposfreopen

引數

指向一個 FILE 物件的指標,該物件標識了流。

返回值



示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* writing errors */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen("myfile.txt","r");
  if (pFile==NULL) perror ("Error opening file");
  else {
    fputc ('x',pFile);
    if (ferror (pFile)) {
      printf ("Error Writing to myfile.txt\n");
      clearerr (pFile);
    }
    fgetc (pFile);
    if (!ferror (pFile))
      printf ("No errors reading myfile.txt\n"); 
    fclose (pFile);
  }
  return 0;
}

該程式開啟一個名為myfile.txt的現有檔案進行讀取,並嘗試對其進行寫入以引發 I/O 錯誤。該錯誤會使用clearerr清除,因此第二次錯誤檢查將返回 false。

輸出
Error Writing to myfile.txt
No errors reading myfile.txt


另見