I know there many subjects about this but none of them helps me.
我知道有很多关于此的主题,但它们都没有帮助我。
I use in my C/C++ project std::cout
and std::cerr
to print info (cout) or error (cerr). But when executing it they don't print in the right order, they seems to "group print". Sometime all cerr then all cout and sometime all cout first then all cerr.
我在我的C / C ++项目std :: cout和std :: cerr中使用打印信息(cout)或错误(cerr)。但是在执行它时,它们不按正确的顺序打印,它们似乎是“分组打印”。有时所有cerr然后所有cout和有时所有cout首先然后所有cerr。
I tried to flush()
after every line, don't work. (luckily, it would be awful having to use it every time ...). Also tried setvbuf(stdout, NULL, _IONBF, 0);
same issue...
我尝试在每一行后刷新(),不起作用。 (幸运的是,每次使用它都会很糟糕......)。还试过setvbuf(stdout,NULL,_IONBF,0);同样的问题......
If run program directly in linux's console, order is good but eclipse console more useful due to colors.
如果直接在linux的控制台中运行程序,顺序很好但是eclipse控制台由于颜色更有用。
Here code sample
这里是代码示例
#include <iostream>
int main(int argc, char** argv)
{
std::cerr << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cout << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cout << __LINE__ << std::endl;
}
And console print
和控制台打印
11
12
14
15
13
16
==> Wrong order ... In this example cerr comes out before cout
==>错误的订单......在这个例子中,cerr在cout之前出现
1 个解决方案
#1
0
Ok the situation is as follows, std::cout
is added into a buffor, and std::cerr
does not. std::cerr
is faster and it does not need to be flushed. Also std::cerr
and std::cout
uses different streams.
好的情况如下,std :: cout被添加到buffor中,std :: cerr没有。 std :: cerr更快,不需要刷新。 std :: cerr和std :: cout也使用不同的流。
The problem here is that std::cerr
shows right away and std:cout
needs to be flushed before it's showed.
这里的问题是std :: cerr立即显示,std:cout需要在显示之前刷新。
#1
0
Ok the situation is as follows, std::cout
is added into a buffor, and std::cerr
does not. std::cerr
is faster and it does not need to be flushed. Also std::cerr
and std::cout
uses different streams.
好的情况如下,std :: cout被添加到buffor中,std :: cerr没有。 std :: cerr更快,不需要刷新。 std :: cerr和std :: cout也使用不同的流。
The problem here is that std::cerr
shows right away and std:cout
needs to be flushed before it's showed.
这里的问题是std :: cerr立即显示,std:cout需要在显示之前刷新。