1、fflush(stdin)是清空輸入緩沖區的意思。stdin就是標準輸入 std即standard(標準),in即input(輸入),合起來就是標準輸入。 壹般就是指鍵盤輸入到緩沖區裏的東西。
2、在清除文件緩沖區時使用,文件以寫方式打開時將緩沖區內容寫入文件。
例:
#include <conio.h>
#include <io.h>
#include <stdio.h>
#include <string.h>
void flush(FILE *stream);
int main(void)
{
FILE *stream;
char msg[] = "This is a test";
/* create a file */
stream = fopen("DUMMY.FIL", "w");
/* write some data to the file */
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf("Press any key to flush DUMMY.FIL:");
getch();
/* flush the data to DUMMY.FIL without closing it */
flush(stream);
printf("\nFile was flushed, Press any key to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
/* flush the stream's internal buffer */
fflush(stream);
/* make a duplicate file handle */
duphandle = dup(fileno(stream));
/* close the duplicate handle to flush the DOS buffer */
close(duphandle);
}
擴展資料
使用fflush的註意事項
1、MSDN 文檔裏也清楚地寫著:fflush on input stream is an extension to the C standard (fflush 操作輸入流是對C標準的擴充)。以下是 C99 對 fflush 函數的定義:int fflush(FILE *stream);
2、如果stream指向輸出流或者更新流(update stream),並且這個更新流最近執行的操作不是輸入,那麽fflush函數將把任何未被寫入的數據寫入stream指向的文件(如標準輸出文件stdout)。
3、fflush函數的行為是不確定的。fflush(NULL)清空所有輸出流和上面提到的更新流。如果發生寫錯誤,flush函數會給那些流打上錯誤標記,並且返回EOF,否則返回0。
4、如果 stream 指向輸入流(如 stdin),那麽 fflush 函數的行為是不確定的。故而使用 fflush(stdin) 是不正確的。
百度百科—fflush(stdin)