-
fflush(stdin)
鎖定
fflush(stdin)注意
fflush(stdin)詳細解釋
函數名: fflush
功能: 清除文件緩衝區,文件以寫方式打開時將緩衝區內容寫入文件
原型:int fflush(FILE *stream)
程序例:
#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返回EOF,數據可能由於寫錯誤已經丟失。當設置一個重要錯誤處理器時,最安全的是用setvbuf函數關閉緩衝或者使用低級I/0例程,如open、close和write來代替流I/O函數。
fflush(stdin)定義
int fflush(FILE*stream);
如果stream指向輸出流或者更新流(update stream),並且這個更新流
最近執行的操作不是輸入,那麼fflush函數將把任何未被寫入的數據寫入stream
fflush(NULL)清空所有輸出流和上面提到的更新流。如果發生寫錯誤,fflush
函數會給那些流打上錯誤標記,並且返回EOF,否則返回0。