When must the output stream in C++ be flushed?

后端 未结 6 1171
执笔经年
执笔经年 2021-01-21 21:45

I understand cout << \'\\n\' is preferred over cout << endl; but cout << \'\\n\' doesn\'t flush the output stream. When

6条回答
  •  野性不改
    2021-01-21 22:26

    I/O libraries buffer data sent to stream for performance reasons. Whenever you need to be sure data has actually been sent to stream, you need to flush it (otherwise it may still be in buffer and not visible on screen or in file).

    Some operations automatically flush streams, but you can also explicitly call something like ostream::flush.

    You need to be sure data is flushed, whenever for example you have other program waiting for the input from first program.

提交回复
热议问题