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)