C++语法基础--标准IO库--输出缓冲管理

1.缓冲区被刷新的情况:
  *程序正常结束
  *缓冲区已满
  *用操作符显示第刷新缓冲区(ends,endl,unitbuf,flush.....)
  *当输出流与输入流关联(tie)时,在读入输入流时将刷新关联输出缓冲区


2.ends和endl的区别
  ends:在缓冲区中插入空(null)字符
       eg:
          cout<<"hello"<<ends<<"how"<<ends<<"are"<<ends<<"you"<<endl;
//hello how are you
  ends:在缓冲区中插入换行(newline)符


3.unitbuf:
   每次执行完操作后都刷新流
   cout<<unitbuf<<"ni"<<"hao"; //等价于 cout<<"ni"<<flush<<"hao"<<flush;


4.nounitbuf将流恢复为正常的,由系统管理的缓冲区刷新方式

5.如果程序崩溃了,则不会刷新缓冲区


6.将输入和输出绑在一起
  tie()原型:
     basic_ostream<Elem, Traits> *tie( ) const;
     basic_ostream<Elem, Traits> *tie( basic_ostream<Elem, Traits> *_Str);

  
  解析:
      其中 basic_ostream<Elem, Traits>代表输出流,basic_ostream真实的定义是:

          template< class CharT, class Traits = std::char_traits<CharT>> 

      class basic_ostream : virtual public std::basic_ios<CharT, Traits>


    
   eg:
     
   int main()    
  {
int x=5;
cin.tie(&cout);
*cin.tie()<<"hello"<<endl;
 //cin.tie()返回cout的指针,所以解引用就相当于cout
cin.tie(NULL);          //解除和cout的绑定
ofstream out("f:/test.txt");
cin.tie(&out);           //绑定out
if(!out)
exit(-1);
while(cin>>x)          //每次输入x值时,out都会把新值写入test.txt,如果cin没有绑定out
{                          //则要等程序正常结束或遇到文件结束符才一次性写入test.txt
out<<x<<'\t';   
}
cin.tie(NULL);        //解除和out的绑定
out.close();
    return 0;
 }

    
  

      

     下集预告:

                      文件的输入和输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值