1.fflush (stdin):
Role: Clean up the standard input stream and throw away the excess data that has not been saved.
Such as:
int main ()
{
int num;
Char str[10];
cin>>num;
cout<<num<<endl;
cin>>str;
cout<<str<<endl;
return 0;
}
Get an integer from stdin into num, then print it out immediately, and get a string from stdin to be stored in STR and print it out immediately. But this may require special consideration: entering two integers in the first line, at the Cin>>num
, the stdin buffer also has an integer that has not been read. Next, wait for the input string, and then save the above number to STR and print it directly. In some ways this is due to an irregular operation, but the program should have a robust
, programmers should prevent this nonstandard operation in advance. You can prompt "Please enter 1 integers" on the program interface, even sometimes it is necessary to stress and warn. Of course, this example is simple and does not work in a UI friendly way.
Chapter. At this point, you can insert Fflush (stdin) before the CIN>>STR statement so that you can empty the extra data in the standard input buffer.
2.fflush (stdout):
Clean up the standard output stream, but instead of dropping the data, it prints the data to the screen in a timely fashion. Standard output is done in units of behavior, and is encountered \ nthe data is printed to the screen. This can cause delays, but windows
Platform, it does not seem to see the difference. Also that is, MSFT has changed the output of stdout into effective in time.
The Fflush function is widely used in the message processing of multithreading and network programming.
Fflush (stdout): empties the output buffer and outputs the buffer content.
Enlighten