Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

The usage of cout.flush( )

leoleo Posts: 13Member
I met a statement in a C++ programme. It's

cout.flush();

Can anyone tell me what it's about? Thanks!

Leo


Comments

  • gautamgautam Posts: 642Member
    : I met a statement in a C++ programme. It's
    :
    : cout.flush();
    :
    : Can anyone tell me what it's about? Thanks!
    :
    : Leo
    :
    :

    It flushes the output.

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : I met a statement in a C++ programme. It's
    :
    : cout.flush();
    :
    : Can anyone tell me what it's about? Thanks!
    :
    : Leo
    :
    :

    When you output something to the screen using buffered I/O, you aren't necessarily guaranteed that the output will show up immediately after the output code is executed. Most of the time the way you write your programs may hide this fact from you but in certain instances you will find yourself wondering if your program is working because you have written out some prompt but can't see anything on the screen. The operating system (I think?) may hold all the text you have written in a buffer and will sometimes only write out the text in that buffer when the buffer gets full. This is done for efficiency sake because it is better to minimize I/O operations as much as possible. The code you mention above forces all the text stored in the buffer to be immediately sent to the screen.

    Buffered I/O happens without you realizing it most times. On the input side of things, if you open a file and read just a single byte from that file, the OS will actually do one huge read operation on the entire sector(cluster?) that the file occupies. In many 32-bit Windows Operating Systems for example, this means reading in a single byte causes the OS to read in 8K bytes or 16K bytes into a buffer, whatever the size of your sector/cluster. Once this single huge read operation has been done, further requests by your program to read in single bytes from the file will actually cause the OS to dole out those byte from the buffer instead of the file. Once your program has read in all the bytes from the current sector/cluster of the file and you are ready to read in the next single byte, the OS will again do a single huge read on the next 8K or 16K bytes into the buffer. If this was not done, then to read a file byte-by-byte, the OS would need to continuosly fetch bytes from the file using disk I/O request which would be EXTREMELY BAD (inefficient/slow).

    Anyway, that's how I understand it. I could be off a little on some details but the principle behind what I've said should still be true.

  • leoleo Posts: 13Member
    I got the ideas. Thank you very much!

    leo

  • DanKNguyenDanKNguyen Posts: 27Member
    so should you use cout.flush() after every cout message if possible?


  • SunlightSunlight Posts: 186Member
    : The operating system (I think?) may hold all the text you have written in a buffer and will sometimes only write out the text in that buffer when the buffer gets full.

    It's actually done by the library, and has nothing to do with the OS, which may or may not implement its own caching.

    --
    [italic][blue]Sunlight[/blue][/italic]


  • SunlightSunlight Posts: 186Member
    : so should you use cout.flush() after every cout message if possible?

    No, just before any place that you absolutely [bold]require[/bold] output (e.g. reading input using getch()). flush() is a relatively expensive operation.
    --
    [italic][blue]Sunlight[/blue][/italic]


  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : so should you use cout.flush() after every cout message if possible?
    :
    :

    If you are using the 'endl' stream manipulator everywhere, like I tend to do, then you certainly don't need to do it since that manipulator automatically does a flush for you. If you write out '
    ' however, this is not done.

    cout << "Blah" << endl; // Automatic flush
    cout << "Blah
    "; // No automatic flush


  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    I've been trying to think of an example where you might run into this buffering problem. Since I'm always using 'endl' at the end of almost everything I write using 'cout' I never really encounter it but I tried this with MSVC++ 6.0 and it illustrates the problem:

    cout << "Press a key to continue!";
    getch();

    Since the output is buffered and we don't use 'endl' here, the program will seem to hang but in reality has already executed the 'cout' and is waiting for a keypress from the user at the 'getch' function. Not until you have already pressed a key do you get to see the prompt asking you to press the key. Include 'iostream' and 'conio' to get this working.

  • DanKNguyenDanKNguyen Posts: 27Member
    : I've been trying to think of an example where you might run into this buffering problem. Since I'm always using 'endl' at the end of almost everything I write using 'cout' I never really encounter it but I tried this with MSVC++ 6.0 and it illustrates the problem:
    :
    : cout << "Press a key to continue!";
    : getch();
    :
    : Since the output is buffered and we don't use 'endl' here, the program will seem to hang but in reality has already executed the 'cout' and is waiting for a keypress from the user at the 'getch' function. Not until you have already pressed a key do you get to see the prompt asking you to press the key. Include 'iostream' and 'conio' to get this working.
    :

    I already use endl at the end of everything too


  • DanKNguyenDanKNguyen Posts: 27Member
    : : I've been trying to think of an example where you might run into this buffering problem. Since I'm always using 'endl' at the end of almost everything I write using 'cout' I never really encounter it but I tried this with MSVC++ 6.0 and it illustrates the problem:
    : :
    : : cout << "Press a key to continue!";
    : : getch();
    : :
    : : Since the output is buffered and we don't use 'endl' here, the program will seem to hang but in reality has already executed the 'cout' and is waiting for a keypress from the user at the 'getch' function. Not until you have already pressed a key do you get to see the prompt asking you to press the key. Include 'iostream' and 'conio' to get this working.
    : :
    :
    : I already use endl at the end of everything too
    :
    :

    OH!!!! Now i remember! I did run into thtat problem when i first started programming, but I just solved it by using endl

Sign In or Register to comment.