C-language function fflush ()

Source: Internet
Author: User

Reprinted from (http://www.cplusplus.com/reference/clibrary/cstdio/fflush) Int fflush (File * stream );

Flush stream

If the givenStreamWas open for writing and the last I/O operation was an output operation, any unwritten data in the output buffer is written to the file.
If it was open for reading and the last operation was an input operation, the behavior depends on the specific library implementation. in some implementations this causes the input buffer to be cleared, but this is not standard behavior.
If the argument is a null pointer, all open files are flushed.
The stream remains open after this call.
When a file is closed, either because of a call to fclose or because the program terminates, all the buffers associated with it are automatically flushed.

Parameters
Stream
Pointer to a file object that specifies a buffered stream.

Return Value

A zero value indicates success.
If an error occurs, EOF is returned and the error indicator is set (see feof ).

Example

In files open for update (I. E ., open for both reading and writting), the stream shall be flushed after an output operation before implements Ming an input operation. this can be done either by repositioning (fseek, fsetpos, rewind)
Or by calling explicitlyFflush, Like in this example:

 
/* Fflush example */# include <stdio. h> char mybuffer [80]; int main () {file * pfile; pfile = fopen ("example.txt", "R +"); If (pfile = NULL) perror ("error opening file"); else {fputs ("test", pfile); fflush (pfile); // flushing or repositioning required fgets (mybuffer, 80, pfile ); puts (mybuffer); fclose (pfile); Return 0 ;}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.