fflush ()- 寫入緩衝區至檔案

格式

#include <stdio.h>
int fflush(FILE *stream);

語言層次

ANSI

安全執行緒

說明

如果可能的話, fflush() 函數會使系統清空與指定輸出 串流相關聯的緩衝區。 如果開啟 串流 進行輸入,則 fflush() 函數會復原任何 ungetc() 函數的效果。 在呼叫之後, 串流 會保持開啟狀態。

如果 串流NULL,系統會清除所有開啟的串流。
附註: 當您關閉串流或程式正常結束而不關閉串流時,系統會自動刪除緩衝區。

回覆值

如果順利刪除緩衝區, fflush() 函數會傳回值 0 。 如果發生錯誤,它會傳回 EOF

錯誤碼的值可以設為:
Value
意義
ENOTOPEN
檔案未開啟。
埃雷西奧
開啟檔案以進行記錄 I/O。
ESTDIN
無法開啟 stdin
EIOERROR
發生非可回復I/O錯誤。
EIORECERR
發生可回復I/O錯誤。

type=record開啟的檔案不支援 fflush() 函數。

範例

此範例會刪除串流緩衝區。
#include <stdio.h>
 
int main(void)
{
   FILE *stream;
   int ch;
   unsigned int result = 0;
 
   stream = fopen("mylib/myfile", "r");
   while ((ch = getc(stream)) != EOF && isdigit(ch))
      result = result * 10 + ch - '0';
   if (ch != EOF)
      ungetc(ch,stream);
 
   fflush(stream);        /* fflush undoes the effect of ungetc function */
   printf("The result is: %d\n", result);
   if ((ch = getc(stream)) != EOF)
      printf("The character is: %c\n", ch);
}

相關資訊