[英]Resetting an ostream, C++
I have 2 different ostreams, one of them cerr, using the same streambuffer, I have some libraries in that might have modified cerr somehow,(flags? format modifiers?). 我有2个不同的ostream,其中一个是cerr,使用相同的streambuffer,我有一些库可能会以某种方式修改cerr,(flags?format modifiers?)。
cerr.rdbuf(&mystreambuffer);
ostream teststream(&mystreambuffer);
cerr << "This " << " is " << " a " << " test";
teststream << "This " << " is " << " a teststream " << " test";
prints: 打印:
This
is
a
test
This is a teststream test
Debugging mystreambuffer
I've noticed that cerr calls mystreambuffer->sync()
every <<
operation while teststream does not call it at all. 调试
mystreambuffer
我注意到CERR调用mystreambuffer->sync()
每次<<
操作,同时teststream不会调用它。
If I am correct cerr
is just an standard ostream, then, why do I see this difference in flushing times? 如果我是正确的
cerr
只是一个标准的ostream,那么,为什么我在冲洗时间看到这种差异? How can I reset cerr back to normal flushing operations? 如何将cerr重置为正常的冲洗操作?
EDIT: I see you guys are commenting about unitbuf and it being default in cerr, but if it was default, wouldn't it write step by step here as well? 编辑:我看到你们正在评论unitbuf并且它在cerr中是默认的,但是如果它是默认的,那么它也不会在这里一步一步地写出来吗?
#include <iostream>
int main(){
std::cerr << "This " << " is " << " a cerr " << " test\n";
std::cout << "This " << " is " << " a cout " << " test\n";
}
Cobain /tmp$ ./test
This is a cerr test
This is a cout test
Try std::cerr.unsetf( std::ios_base::unitbuf );
试试
std::cerr.unsetf( std::ios_base::unitbuf );
. 。 That flag is on for
cerr
by default. 默认情况下,该标志为
cerr
。
ios::unitbuf flag is the reason for that which is set to default for cerr. ios :: unitbuf标志是cerr默认设置的原因。
You need to use the nounitbuf manipulator in order to fix it. 您需要使用nounitbuf操纵器来修复它。 Some older libraries may not have it, if so then use unsetf.
一些较旧的库可能没有它,如果是,那么使用unsetf。
Edit: Default setting for unitbuf is implementation-dependent :) 编辑:unitbuf的默认设置是依赖于实现的:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.