重新学习《C++Primer5》第8章-IO库

来源:互联网 发布:知乎豆瓣高分书籍 编辑:程序博客网 时间:2024/06/06 04:21

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;}
0 0