【发布时间】:2011-08-26 23:44:41
【问题描述】:
#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";之前 -
我想知道为什么 cout 的输出在 cerr 之前。我认为cout的输出是缓冲的,cerr的输出应该先出来
标签: c++