#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int fileTovector(string fileName, vector<string>& svcc)
{
ifstream inFile(fileName.c_str());
if (!inFile)
{
return 1;
}
string s;
while(getline(inFile, s))
svcc.push_back(s);
inFile.close();
if (inFile.eof())
{
return 4;
}
if (inFile.bad())
{
return 2;
}
if (inFile.fail())
{
return 3;
}
}
int main()
{
vector<string> svec;
string filename, s;
cout << "fileName:" << endl;
cin >> filename;
int i = fileTovector(filename, svec);
cout << i << endl;
string word;
istringstream isstream;
for (vector<string>::iterator iter = svec.begin();
iter != svec.end(); ++iter)
{
isstream.str(*iter);//将 string 类型的 s 复制给 strm
while(isstream >> word)//istringstream对象以空格或文件结束符为隔断,因此单词中可能会包含其他标点符号如“,”等
{
cout << word << endl;
}
isstream.clear();
}
return 0;
}
文件中的每一行存储在 vector容器对象中,读取每行的单词
最新推荐文章于 2020-08-06 21:06:20 发布