Fflush (stdin) and fflush (stdout)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/yeyuangen/article/details/6743416

Fflush (stdin) cleans up the standard input stream and discards the excess data that is still not saved.

Fflush (stdout): empties the output buffer and outputs the buffer contents

For example, the following small program:

void Main ()

{

int A;

Char str[10];

cin>>a;

cout<<a<<endl;

cin>>str;

cout<<str<<endl;

}

The goal is simple: to get an integer from stdin to a, then print it out immediately; get a string from stdin into str and print it out immediately. But the following may require special consideration: two integers were entered on the first line, and after Cin>>a, the stdin buffer had an integer that was not read. Next, the input string is not equal, and the number above is stored directly in STR and printed.

To some extent this is caused by the operation of the non-standard, but the program should be robust, the programmer should prevent this irregular operation in advance. You can prompt "Please enter 1 integers" on the program interface, and sometimes it is necessary to take pains to emphasize and warn. Of course, this example is for simplicity and is not a UI-friendly aspect of the fuss. At this point, you can insert Fflush (stdin) before the CIN>>STR statement, so that the extra data in the standard input buffer can be emptied.

Fflush (stdout) is similar to Fflush (stdin), which cleans up the standard output stream, but it does not discard the data, but it prints the data to the screen in a timely manner. In order to understand it better, you need to know the fact that the standard output is in "line" units, that is, it touches \ n to print data to the screen. This can cause delays, such as the following lines of code:

int A;

printf_s ("Input one number:");

Fflush (stdout); \ \ #1

scanf_s ("%d", &a);

Without the code #, you might not see "input one number" printed on some platforms because it doesn't have a carriage return. At this time, Fflush (stdout) plays a role in the timely output.

But on the Windows platform, there seems to be no difference. Also that MSFT has changed the output of stdout to take effect in time.

Fflush functions are widely used in multi-threaded, network-programmed message processing.

Fflush (stdin) and fflush (stdout)

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.