8.1 IO类
1.IO对象不能拷贝或赋值
ofstream out1,out2;
out1=out2;//错误:不能对流对象赋值
ofstream print(ofstream);//错误
out2=print(out2);//错误
因此通常以引用方式传递和返回流。读写一个对象通常会改变其状态,所以不能是const reference。
2.文件的输入输出
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
ifstream& read(ifstream& in)
{
string str;
vector<string> vstr;
while (getline(in,str),!in.eof())
{
vstr.push_back(str);
}
for_each(vstr.cbegin(),vstr.cend(),
[](const string& str){cout<<str<<endl;});
return in;
}
int main()
{
ifstream in("test.txt");
read(in);
getchar();
return 0;
}