Fflush (stdin) and fflush (stdout)

Source: Internet
Author: User
Tags stdin

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

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.