Home Technique fflush

fflush



Overview

Function name: fflush

Function: Clear the read and write buffer, when the data in the output buffer needs to be physically written immediately

Header file: stdio.h

Prototype: intfflush(FILE*stream)

where stream is the stream to be flushed

function description h2>

If the pointer points to an output stream or an update stream whose most recent operation is not an input, the output refresh will create any unwritten data to the stream that will be written to the file and the stream where the most recent data has been modified. , And the last file status change should be marked as the timestamp of the updated base file.

For a stream that is opened for reading using the basic file description, if the file is not yet in EOF and the file is a searchable file, the file offset of the basic open file description should be set to the stream The file location, and any characters pushed back to the stream by ungetc() or ungetwc() that have not been read from the stream will be discarded (no further changes to the file offset). [End of option]

If stream is a null pointer, fflush() will perform this flushing operation on all streams with the behavior defined above.

Program example

#include#include#include#includevoid flush(FILE *stream);int main(void){ FILE *stream; char msg[]="Thisisatest"; /*createafile*/ stream=fopen("DUMMY.FIL","w");/*writesomedatatothefile*/ fwrite(charmsg,strlen(charmsg),1,stream); clrscr(); printf("PressanykeytoflushDUMMY.FlushDUMMY.FIL) "); getch();/*flushthedatatoDUMMY.FILwithout Closingit*/ flush(stream); printf("\nFilewasflushed,Pressanykey\toquit:"); voidstream(); 0 FILE* lush() return{getch(); 0 int duphandle; /*flushthestream'sinternalbuffer*/ fflush(stream);/*makeaduplicatefilehandle*/ duphandle=dup(fileno(stream));/*closetheduplicatehandletoflushthebuffer)*/ phandle(duphandle) 

If the refresh is successful, fflush returns 0. If the specified stream has no buffer or is read-only open, a value of 0 is also returned. Returning EOF indicates an error.

Note: If fflush returns EOF, the data may have been lost due to a write error. When setting up an important error handler, it is safest to close the buffer with the setvbuf function or use low-level I/0 routines such as open, close, and write instead of stream I/O functions.

Other usage

fflush(stdin) refreshes the standard input buffer and discards the contents of the input buffer [non-standard]

< p>fflush(stdout) flushes the standard output buffer, and prints the contents of the output buffer to the standard output device

Notes

It is never defined in the C and C++ standards Pass fflush(stdin). Someone might say: "But I used flush(stdin) to solve this problem, how can you say it is wrong?" Indeed, some compilers (such as VC6) support using fflush(stdin) to clear the input buffer, but Not all compilers need to support this feature (gcc under linux does not support it, tested by my GCC 4.6.2), because the standard does not define fflush (stdin) at all.

The MSDN document also clearly states:

fflushoninputstreamisanextensiontotheCstandard (fflush operation input stream is an extension of the C standard).

The following is the definition of fflush function in C99:

intfflush(FILE*stream);

If stream points to output stream or update stream (updatestream), and The most recent operation performed by this update stream is not input, so the fflush function will write any unwritten data to the file pointed to by the stream (such as the standard output file stdout). Otherwise, the behavior of the fflush function is undefined. fflush (NULL) clears all output streams and the update streams mentioned above. If a write error occurs, the flush function will mark those streams as an error and return EOF, otherwise it returns 0.

It can be seen that if stream points to an input stream (such as stdin), then the behavior of the fflush function is undefined. Therefore, it is incorrect to use fflush(stdin).

This article is from the network, does not represent the position of this station. Please indicate the origin of reprint
TOP