iostream 中的缓冲

发布于 2024-09-25 05:31:50 字数 87 浏览 0 评论 0原文

有人可以解释一下,这到底是什么意思?我试图了解 clogcerr 之间的区别,区别仅在于缓冲 提前致谢

can somebody please explain, what exactly does it mean? I'm trying to understand what is the difference between clog and cerr, difference is only in buffering
thanks in advance

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

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

发布评论

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

评论(2

椵侞 2024-10-02 05:31:50

当您写入 clog 时,您实际上写入了内存中的字符存储。当此存储已满时,它会写入实际流。当流关闭时(这会在程序结束时发生),则所有剩余数据都会写入流中(也可能发生手动刷新)。

对于 cerr,这种情况不会发生。

通常,由于写入内存比写入大多数流要快,因此缓冲的效果是性能的整体提高。情况并非总是如此(一个典型的例子是间接级别导致缓冲区写入缓冲区写入缓冲区),但这通常足以成为合理的默认值。但结果是,正在写入的流对象和正在写入的实际流之间可能存在延迟,这在某些情况下是不合适的。

可以合理地想象,写入 cerr 的内容可能需要立即执行操作,但写入阻塞的内容则不需要,因此存在差异。

When you write to clog then you actually write to an in-memory store of characters. When this store becomes full, it then writes to the actual stream. When the stream is closed, (which would happen on the program ending) then any remaining data is written to the stream (manual flushing can also happen).

With cerr, this does not happen.

As a rule, since writing to memory is faster than writing to most streams, the effect of buffering is an overall improvement in performance. This isn't always the case (a classic example is where levels of indirection lead to a buffer writing to a buffer writing a buffer), but it is often enough for this to be a reasonable default. A consequence though is that there can be a delay between the stream object being written to, and the actual stream being written to, which is inappropriate in some cases.

It's reasonable to imagine that something writing to cerr may need prompt action, but something writing to clog will not, hence the difference.

哆兒滾 2024-10-02 05:31:50

clog 和 cerr 之间的区别在于 clog 是完全缓冲的,而 cerr 的输出是在每次格式化后写入外部设备。对于完全缓冲的流,仅当缓冲区已满时才会写入实际外部设备的输出。因此 clog 对于将输出重定向到文件更有效,而 cerr 主要用于终端 I/O。每次格式化后写入外部设备(在 cerr 的情况下写入终端)的目的是同步终端的输出和输入。此外,预定义的流与其关联的 C 标准文件同步。

The difference between clog and cerr is that clog is fully buffered, whereas output to cerr is written to the external device after each formatting. With a fully buffered stream, output to the actual external device is written only when the buffer is full. Thus clog is more efficient for redirecting output to a file, while cerr is mainly useful for terminal I/O. Writing to the external device after every formatting, to the terminal in the case of cerr, serves the purpose of synchronizing output to and input from the terminal. Also, the predefined streams are synchronized with their associated C standard files.

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