打开读取文件

 1 // 320.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <string>
 7 #include <fstream>
 8 using namespace std;
 9 istream& get(istream& in);
10 
11 ifstream& open_file(ifstream& ifile,string name)
12 {
13     ifile.close();
14     ifile.clear();
15     ifile.open(name.c_str());
16     return ifile;
17 }
18 
19 int main(int argc, char* argv[])
20 {
21 
22     ifstream ifile;
23     if(open_file(ifile,"C:\\aa.txt"))
24     {
25         get(ifile);
26     }
27 
28     ifile.close();
29 
30     get(cin);
31 
32     system("pause");
33     return 0;
34 }
35 
36 istream& get(istream& in)
37 {
38     int ival;
39     while(in>>ival,!in.eof())
40     {
41         if(in.bad())
42             throw runtime_error("sfas");
43         if(in.fail())
44         {
45             cerr<<"error"<<endl;
46             in.clear();
47             in.ignore(200,' ');
48             continue;
49         }
50         cout<<ival<<'\t';
51     }
52     in.clear();
53     return in;
54 }

 

posted @ 2013-03-20 23:07  何合  阅读(214)  评论(0编辑  收藏  举报