#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)) svec.push_back(s); inFile.close(); if(inFile.eof()) return 4; if(inFile.bad()) return 2; if(inFile.fail()) return 3; cout<<"fileToVector()返回了0"<<endl;//debug:检查分支的完备性 return 0;// } 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; } //debug:检验 cout<<"Vector:"<<endl; for(vector<string>::iterator iter=svec.begin();iter!=svec.end();++iter) cout<<*iter<<endl; return 0; }
C++ primer 习题练习:8.9
最新推荐文章于 2023-07-15 20:28:06 发布