#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
#include <cstdio>
int main( )
{
char pbuffer[BUFSIZ];
setbuf(stdout, pbuffer);
cout << "hello cout" ;
sleep(5);
cerr << "hello cerr";
sleep(5);
cout << "\nAll done " << endl;
sleep(5);
return 0;
}
编译并运行上述程序后,它的输出如下:
hello couthello cerr
All done
我认为应该是这样的:
但我认为它应该是:
hello cerrhello cout
All done
我想知道为什么
cerr
会刷新cout
的缓冲区?
cout << "hello cout";
在cerr << "hello cerr";
之前。 - iammilind