#include<iostream>
#include<vector>
#include<string>
#include <fstream>
#include <sstream>
using namespace std;
int FiletoVector(const string& ,vector<string> &); //声明函数
int main()
{
vector<string> svec;
string filename;
cout<<"Please enter the filename"<<endl;
cin>>filename;
switch (FiletoVector(filename,svec)) //函数调用
{
case 0:
break;
case 1:
cout<<"open file fail"<<endl;
system("pause");
return 1;
case 2:
cout<<"系统故障"<<endl;
system("pause");
return 1;
case 3:
cout<<"读取数据失败"<<endl;
system("pause");
return 1;
}
string word;
istringstream stream; //定义字符串流对象,用于逐一读取每个单词
for(vector<string>::iterator iter=svec.begin();iter!=svec.end();++iter)
{
stream.str(*iter); //将*iter复制给stream
while(stream>>word) //读取每个string
{
cout<<word<<endl;
}
stream.clear(); //清空stream流
}
system("pause");
return 0;
}
int FiletoVector(const string& filename,vector<string>& svec)
{
ifstream infile;
string s;
infile.open(filename.c_str(),iostream::in); //打开名字为 filename的文件
if(1!=infile.is_open())
return 1;
while(getline(infile,s)) //读取文件,每行数据都存在s中
svec.push_back(s);
infile.close();
if(infile.eof()) //遇到文件结束符
return 0;
if(infile.bad()) //系统故障
return 2;
if(infile.fail()) //读入文件失败
return 3;
}