#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;
}
的緩衝後,我編譯和運行上面的程序,它的輸出是:爲什麼CERR刷新COUT
hello couthello cerr
All done
,但我認爲它應該是:
hello cerrhello cout
All done
我想知道,爲什麼cerr
沖刷了cout
的緩衝區?
'cout <<「hello cout」;'before before'cerr <<「hello cerr」;' – iammilind 2011-05-17 06:28:37
我想知道爲什麼cout的輸出會在cerr之前出現。我認爲cout的輸出是緩衝的,cerr的輸出應該先出來 – wildpointercs 2011-05-17 06:30:58