c++ 输入输出流关联
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
cout << cin.tie() << endl; //打印cin的关联流的地址
cout << &cout << endl; //打印cout的地址
cout << &cin << endl; //打印地址
cout << cout.tie() << endl; //打印关联流地址
cout <<"tie the cerr to cout " << endl;
cout.tie(&cerr); //把cerr关联到cout
cout << cout.tie() << endl; //打印cout的关联流,即打印cerr的地址
cout << &cerr <<endl; //打印cerr的地址
cout.tie(nullptr); //彻底解开关联流,关联流都被解开了把?
cout << "free the tie:"<<cout.tie() <<endl; //
cout.tie(nullptr); //解开cout 的关联流
cout << cout.tie() <<endl;
cout << "tie the new:" <<endl;
ostream * old_tie = cin.tie(nullptr); //解开cin的挂链流,并且把cin原来的关联流返回
cout << "old value: "<< old_tie << endl; //
cout << "now 's cin ' s tie is:" <<cin.tie()<< endl;
return 0;
}
几个总结:
(1)无论是关联还是解开,都是被关联的流作为主要职责。即调用tie的那个流作为主要的行使权利,它如果要其它流关联到它,那么会调用tie函数,strm.tie(& strm2) 把strm2关联到strm上
(2)如果要解开流的关联,那么strm调用tie来解开。strm.tie(nullptr)则可以了。
(3)谁调用关联,那么谁就调用tie(nullptr)去解开关联。并且这个流如果执行,那么被关联流strm2会首先清除缓存
与50位技术专家面对面20年技术见证,附赠技术全景图
总结
以上是生活随笔为你收集整理的c++ 输入输出流关联的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: PHP+MySQL能做什么?
- 下一篇: C++顺序容器之deque初探