[c++]代码库
#include <iostream>
#include <fstream>
#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))//infile>>s 前者是输出一行一行的文本,用这个只是输出一个一个的单词
svec.push_back(s);
infile.close();
infile.clear();
if (infile.eof())
return 4;
if (infile.bad())
return 2;
if (infile.fail())
return 3;
}
int main()
{
vector<string> svec;
string fileName,s;
cout<<"Enter fileName:"<<endl;
cin>>fileName;
switch(fileToVector(fileName,svec))
{
case 1:
cout<<"error: can not open file:" <<fileName<<endl;
return -1;
case 2:
cout<<"error:system failure"<<endl;
return -1;
case 3:
cout<<"error:read failure" <<endl;
return -1;
default:
break;
}
cout<<"Vector"<<endl;
for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
cout<<*iter<<endl;
return 0;
}