学习文件流(2)

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 void process(string s)
 9 {
10 
11     cout << s << endl;
12 }
13 
14 int main()
15 
16 {
17     vector <string> files;
18     files.push_back("one.txt");
19     files.push_back("two.txt");
20     files.push_back("three.txt");
21 
22     string s;
23     vector<string>::const_iterator it = files.begin();
24     while(it != files.end())
25     {    
26         ifstream input(it->c_str());//用迭代出来的文件名来创建一个流
27         if(!input)//打开一个文件失败
28         {
29             cerr << "error: can not open file:"
30                    << *it << endl;
31             input.clear();
32             ++it;
33             continue;
34         }
35         while(input >> s)//打开一个文件成功
36             process(s);
37         input.close();
38         input.clear();
39         ++it;
40 
41     }
42 
43     return 0;
44 
45 
46 }

 

posted @ 2012-12-23 11:43  uniquews  阅读(147)  评论(0编辑  收藏  举报