[c++]代码库
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int fileToVector(string fileName)
{
ifstream infile(fileName.c_str());
if (!infile)
return 1;
string s;
vector<string> svec;
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;
for (vector<string>::iterator it = svec.begin(); it != svec.end(); ++it)
cout<<*it<<endl;
}
int main()
{
vector<string> files;
string fileName;
cout<<"Enter fileName:(Ctrl+Z to end)"<<endl;
while (cin>>fileName)
files.push_back(fileName);
vector<string>::iterator iter = files.begin();
while (iter != files.end())
{
switch(fileToVector(*iter))
{
case 1:
cout<<"error: can not open file:" <<*iter<<endl;
break;
case 2:
cout<<"error:system failure"<<endl;
break;
case 3:
cout<<"error:read failure" <<endl;
break;
default:
break;
}
++iter;
}
return 0;
}