The Fflush function inside C

Source: Internet
Author: User

NAME
Fflush-flush a Stream

Synopsis
#include <stdio.h>

int fflush (FILE *stream);

DESCRIPTION
For output streams, fflush () forces a write of all user-space buffered data for the given output or update stream via the Stream ' s underlying write function.

For-input streams, fflush () discards any buffered data, have been fetched from the underlying file, but have not been co Nsumed by the application. The open status of the stream is unaffected.

If the given file stream is an output stream, then fflush () writes the contents of the output to the buffer to the file . If the given file stream is of type input, then fflush () clears the input buffer.

  Fflush () is useful when debugging, especially if an error fragment occurs before the output to the screen in the program. The direct call to the Fflush (STDOUT) output guarantees that your debug output can be output at the correct time.

If The stream argument is NULL, fflush () flushes all open output streams.

For a nonlocking counterpart, see Unlocked_stdio (3).

1.

Flush (stdin) refreshes the standard input buffer and discards anything in the input buffer.
Fflush (STDOUT) refreshes the standard output buffer and prints the output buffer to the standard output device.

2.

The real function of fflush is to immediately output the contents of the buffer to the device. Because of this, you can only use fflush when writing to a file. Using Fflush when reading a file is not a good programming method, because that code might work in some environments, but in other circumstances it would be an error, causing the program to be non-portable.

Flush, which empties the buffer and uses caching on a slow device, is a means of boosting data processing efficiency, and flushing is actually writing the buffered content in memory to external memory media
See MSDN's kernel32! FlushFileBuffers

Fclose may not flush once after the operation, the operating system will execute flush when the CPU is idle

3.

Fflush should not be used when reading a file, but should be used when writing to a file.
Fflush empties the buffer, and fclose empties the buffer before closing the file. If you exit the program using the Exit function, or the main function returns, all files that have not been closed after opening are automatically closed, and the buffers are emptied when they are closed.

Normally, the buffer will not be purged until the program is not properly terminated.

Reference

Fflush function http://blog.chinaunix.net/uid-21651805-id-3482290.html

What does the Fflush function do? http://blog.csdn.net/stpeace/article/details/9063293

The Fflush function inside C

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.