Reset ostream, C ++

I have 2 different ostreams, one of them is cerr, using the same streambuffer, I have several libraries that could modify cerr somehow (modifiers format flags?).

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 mystreambufferI noticed that cerr calls mystreambuffer->sync()every operation <<, while the test thread doesn't call it at all.
If Iโ€™m right, cerritโ€™s just a standard stream, then why do I see this difference during flushing? How can I reset cerr to return to normal flushing operation?

EDIT: I see that you guys are commenting on unitbuf and it is used by default in cerr, but if it was by default, could it also write step by step here?

#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
+3
2

std::cerr.unsetf( std::ios_base::unitbuf );. cerr .

+1

ios:: unitbuf , cerr .

nounitbuf . , unsetf.

: unitbuf :)

+1

Source: https://habr.com/ru/post/1776866/


All Articles