我有 2 个不同的 ostream,其中一个 cerr,使用相同的流缓冲区,我有一些库可能以某种方式修改了 cerr,(标志?格式修饰符?)。
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
的只是一个标准的 ostream,那么,为什么我会在冲洗时间上看到这种差异?如何将 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