题
我有2种不同的水溶液,其中一个是Cerr,使用相同的streambuffer,我有一些库可能已修改过Cerr(flags?格式化修饰符?)。
cerr.rdbuf(&mystreambuffer);
ostream teststream(&mystreambuffer);
cerr << "This " << " is " << " a " << " test";
teststream << "This " << " is " << " a teststream " << " test";
印刷:
This
is
a
test
This is a teststream test
调试 mystreambuffer
我注意到CERR电话 mystreambuffer->sync()
每一个 <<
操作虽然TestStream根本不称呼它。
如果我正确 cerr
那么,这只是一个标准的斯特里姆,为什么我会看到冲洗时间的这种差异呢?如何将Cerr重置为普通的冲洗操作?
编辑: 我看到你们在评论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
解决方案
尝试 std::cerr.unsetf( std::ios_base::unitbuf );
. 。那个标志正在启动 cerr
默认情况下。
其他提示
iOS :: unitbuf标志是设定CERR默认值的原因。
您需要使用NounitBuf操纵器来修复它。一些较旧的库可能没有它,如果是这样,请使用unsetf。
编辑:unitbuf的默认设置与实现有关:)
不隶属于 StackOverflow