批量处理文件

 1 #include <iostream>
 2 #include <fstream>
 3 #include <cstdlib>
 4 
 5 using std::cout;
 6 using std::endl;
 7 using std::fstream;
 8 using std::string;
 9 
10 int main(int argc, char **argv)
11 {
12     //创建文件名列表文件,若存在则清空文件
13     fstream file_list("file_list.txt",std::ios::out);
14     file_list.close();
15 
16     //写入文件名列表到file_list.txt
17     system("dir /a /b >> file_list.txt");
18     
19     long sum_code = 0;
20     fstream code_file;
21     file_list.open("file_list.txt", std::ios::in);
22     string str_line = "";
23     string t_str = "";
24     unsigned int loc = 0;//查找文件名中的"."
25     string str_last = "";
26     
27     while (!file_list.eof())
28     {
29         getline(file_list,str_line);
30         loc = str_line.find(".",0);//从0开始查找"."出现的位置
31 //        cout << "loc = " << loc << endl;
32         if (loc != string::npos)
33         {
34             str_last = str_line.substr(loc);
35 //            cout << "str_last = " << str_last << endl;
36         }
37         else
38         {
39             continue;
40         }
41         
42         if (str_last.compare(".h") == 0
43         || str_last.compare(".c") == 0
44         || str_last.compare(".cpp") == 0)
45         {
46             code_file.open(str_line.c_str(),std::ios::in);
47             cout << "文件名 : " << str_line << endl;
48         }
49         else
50         {
51             continue;
52         }
53         
54         //读文件行数
55         while (!code_file.eof())
56         {
57             getline(code_file,t_str);
58             cout << t_str << endl;
59             sum_code++;
60         }
61         code_file.close();
62         cout << endl;
63     }
64     file_list.close();
65     
66     cout << "代码行数:" << sum_code << endl;
67     system("pause");
68 
69     return 0;
70 }

 

posted @ 2016-12-21 16:33  Aristotle_pass_!  阅读(151)  评论(0编辑  收藏  举报