IO库 8.4

题目:编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中。

 #include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std; void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs) {
string buf;
while (getline(ifs, buf)) {
vec.push_back(buf);
}
}
} int main()
{
vector<string> vec;
ReadFileToVec("data.txt", vec);
for (const auto& str : vec)
cout << str << endl;
}
return ;
}
上一篇:docfx (一)


下一篇:iOS开发UITableViewCell的选中时的颜色设置