- 论坛徽章:
- 0
|
有哪位大神看一下吗?- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <stdexcept>
- #include <string>
- using namespace std;
- void readIntergerFile(const string& filename,vector<int>& dest)throw(invalid_argument,runtime_error)
- {
- ifstream in;
- int temp;
- in.open(filename.c_str());//这个函数,我查了一下是返回const string指针,应该可以这么用吧
- if(in.fail())
- {
- string error="fail to open file"+filename;//总是说不能打开文件
- throw invalid_argument(error);
- }
- while(in>>temp)
- {
- dest.push_back(temp);
- }
- if(in.eof())
- {
- in.close();
- }else{
- string error ="unable to read file";
- throw runtime_error(error);
- }
- }
- int main(){
- vector<int> dest;
- const string filename="a.txt";
- try{readIntergerFile(filename,dest);
- }catch(invalid_argument& e){
- cerr<<e.what()<<endl;
- exit(1);
- }catch(runtime_error& e){
- cerr<<e.what()<<endl;
- exit(1);
- }
- for(unsigned int i=0;i<dest.size();i++)
- {
- cout<<dest[i]<<" ";
- }
- cout<<endl;
- return (0);
- }
复制代码 |
|