*程序正常结束
*缓冲区已满
*用操作符显示第刷新缓冲区(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;
}
下集预告:
文件的输入和输出