Can I stop std::cout flushing on “\n”?

后端 未结 3 2003
感动是毒
感动是毒 2020-12-06 07:21

According to to this post std::cout will automatically flush on \\n when it is attached to an interactive device (e.g. a terminal window). Otherwise (e.g. when

3条回答
  •  情书的邮戳
    2020-12-06 08:01

    This is not an issue with C++ (there is no language requirement that \n flushes anything) but with your operating system and/or console software. If the console wants to flush its buffer when it sees a newline, then it can, and I would guess that most do so. Note that it is important to differentiate between the C++ runtime's buffers (which can be to some extent controlled from your C++ code) and the buffers of the console application (over which it has no control).

    FYI, there is a flag in the standard iostream library called unitbuf which if set causes the buffers to be flushed after each output operation. It is set, for example, for the std::cerr stream. This has nothing to do with the '\n' character however, as you can output multiple '\n' s in a single operation.

提交回复
热议问题