#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
int filetovector(string filename,vector<string>& svec)
{
ifstream infile(filename.c_str());
if(!infile)
return 1;
string s;
while(getline(infile,s)) //读取文件的一行字幅
{
svec.push_back(s);
}
infile.close();
if(infile.eof()) return 4; //文件结束符
if(infile.bad()) return 2; //发生系统故障
if(infile.fail()) return 3; //read data error
}
int main()
{
vector<string> svec;
string filename,s;
cout<<"enter filename : "<<endl;
cin>>filename;
switch(filetovector(filename,svec))
{
case 1:
cout<<"error : cannot to open file: "
<<filename<<endl;
return -1;
case 2:
cout<<"error :system failure "<<endl;
return -1;
case 3:
cout<<"error : read failure "<<endl;
return -1;
}
string word;
istringstream isstream; //读取每行,操纵每个单词
for(vector<string>::iterator iter = svec.begin();iter != svec.end();++iter)
{
isstream.str(*iter);
while(isstream>>word)
cout<<word<<endl;
}
isstream.clear();
system("pause");
return 0;
}