我可以停止 std::cout 对“\n”的刷新吗?

发布于 2024-07-17 09:47:15 字数 303 浏览 12 评论 0原文

根据 这篇文章 std::cout 将自动刷新\n 当它附加到交互式设备(例如终端窗口)时。 否则(例如,当通过管道传输到文件时)它将完全缓冲,并且仅在 .flush()std::endl 上刷新。

有没有办法在 Microsoft Visual C++ 中覆盖此行为,以便我可以选择是否需要完全缓冲模式或行缓冲模式?

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 being piped to a file) it will act fully buffered and will only flush on .flush() or std::endl.

Is there a way to override this behaviour in Microsoft Visual C++ so that I can select whether I want fully buffered or line buffered mode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋凉 2024-07-24 09:47:15

与 anon(2009 年 4 月 28 日)的回答相反,此行为与操作系统或“控制台软件”无关。

C++ 的 流设计为可与 C 的 流互操作。 目标是允许将 std::cout 的使用与 printf/puts 的使用混合在一起。 为了实现这一点,std::coutstreambuf 是在 C 的 stdout 流之上实现的。 当标准输出连接到终端设备时,实际上是 C 的 stdout 进行行缓冲。

您可以调用 std::ios_base::sync_with_stdio(false)(在您的程序使用任何 C++ 的标准 I/O 流之前)告诉 C++ 流库直接与底层文件描述符通信,而不是分层在 C 的流库之上。 这完全避免了 C 的 stdout 流并加速了 C++ 的 I/O 流,但代价是两个库不再能够很好地混合。

另一种方法是通过调用 无条件地将 stdout 设置为完全缓冲std::setvbuf(stdout, nullptr, _IOFBF, BUFSIZ)。 然后,即使 std::cout 仍然通过 stdout 写入,您也不会在每个换行符之后刷新 stdout

Contrary to anon's (Apr 28 '09) answer, this behavior has nothing to do with the operating system or "console software."

C++'s <iostream> streams are designed to be interoperable with C's <stdio.h> streams. The goal is to allow uses of std::cout to be intermixed with uses of printf/puts. To achieve this, std::cout's streambuf is implemented atop C's stdout stream. It is actually C's stdout that is line-buffered when the standard output is attached to a terminal device.

You can call std::ios_base::sync_with_stdio(false) (before your program uses any of C++'s standard I/O streams) to tell the C++ streams library to communicate directly with the underlying file descriptors rather than layering atop C's streams library. This avoids C's stdout stream entirely and speeds up C++'s I/O streams at the cost of the two libraries no longer mixing well.

An alternative is to unconditionally set stdout to fully buffered by calling std::setvbuf(stdout, nullptr, _IOFBF, BUFSIZ). Then, even though std::cout is still writing through stdout, you will not have stdout flushing after every newline.

可爱暴击 2024-07-24 09:47:15

这不是 C++ 的问题(没有刷新任何内容的语言要求),而是操作系统和/或控制台软件的问题。 如果控制台在看到换行符时想要刷新其缓冲区,那么它可以,而且我猜大多数人都会这样做。 请注意,区分 C++ 运行时的缓冲区(在某种程度上可以由 C++ 代码控制)和控制台应用程序的缓冲区(它无法控制)非常重要。

仅供参考,标准 iostream 库中有一个名为 unitbuf 的标志,如果设置该标志,则会在每次输出操作后刷新缓冲区。 例如,它是为 std::cerr 流设置的。 然而,这与 '\n' 字符无关,因为您可以在单个操作中输出多个 '\n' 。

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.

鱼忆七猫命九 2024-07-24 09:47:15

只要认为合适,实现就可以自由刷新。 是否刷新 \n 因供应商而异。

我可以看到名为 ios_base& 的东西 nounitbuf(ios_base& str); 来自我的 C++0x 草案。 试一试。 这是标准 C++ 为您提供的唯一东西。

An implementation is free to flush whenever it feels it is appropriate. It varies from vendor to vendor whether they flush on \n or not.

I can see something called ios_base& nounitbuf(ios_base& str); from my C++0x draft. Give it a shot. This is about the only thing that standard C++ gives you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文