加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么cerr刷新cout的缓冲区

发布时间:2020-12-16 04:51:48 所属栏目:百科 来源:网络整理
导读:#include iostreamusing std::cout;using std::endl;using std::cerr;#include cstdioint main( ){ char pbuffer[BUFSIZ]; setbuf(stdout,pbuffer); cout "hello cout" ; sleep(5); cerr "hello cerr"; sleep(5); cout "nAll done " endl; sleep(5); return
#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的缓冲区?

解决方法

这是设计的.

cin和cerr都绑定到cout,并在任何自己的操作之前调用cout.flush().

这个想法可能是输入和输出应该以正确的顺序发生.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读