简体   繁体   中英

Eclipse C++ console print order linux

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.

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...

If run program directly in linux's console, order is good but eclipse console more useful due to colors.

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

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.

The problem here is that std::cerr shows right away and std:cout needs to be flushed before it's showed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM