C++文件的批处理

C++文件的批处理

时间:2012-07-23 09:43 点击:45 次 【    

一、统计代码行数

  几个星期前做了个统计代码的小程序,对文件的批处理需要获取文件列表,因为是在windows下操作的,所以百度到一个系统调用

    system("dir /a /b >> file_list.txt");

功能就是把当前目录下的所有文件名写到file_list.txt文件中去。这样我就得到了所有的文件名,从而就可以对文件批处理了。对每一个文件的操作就是首先读取文件名,然后就打开文件,接着一行一行的读文件,没读取一行文件就累计代码行数。过程很简单,代码如下:

#include <iostream>
 #include <fstream>
 #include <cstdlib>
 
 using std::cout;
 using std::endl;
 using std::fstream;
 using std::string;
 
 int main(int argc, char **argv)
 {
     //创建文件名列表文件,若存在则清空文件
     fstream file_list("file_list.txt",std::ios::out);
     file_list.close();
 
     //写入文件名列表到file_list.txt
     system("dir /a /b >> file_list.txt");
     
     long sum_code = 0;
     fstream code_file;
     file_list.open("file_list.txt", std::ios::in);
     string str_line = "";
     string t_str = "";
     unsigned int loc = 0;//查找文件名中的"."
     string str_last = "";
     
     while (!file_list.eof())
     {
         getline(file_list,str_line);
         loc = str_line.find(".",0);
 //        cout << "loc = " << loc << endl;
         if (loc != string::npos)
         {
             str_last = str_line.substr(loc);
 //            cout << "str_last = " << str_last << endl;
         }
         else
         {
             continue;
         }
         
         if (str_last.compare(".h") == 0
         || str_last.compare(".c") == 0
         || str_last.compare(".cpp") == 0)
         {
             code_file.open(str_line.c_str(),std::ios::in);
             cout << "文件名 : " << str_line << endl;
         }
         else
         {
             continue;
         }
         
         //读文件行数
         while (!code_file.eof())
         {
             getline(code_file,t_str);
             cout << t_str << endl;
             sum_code++;
         }
         code_file.close();
         cout << endl;
     }
     file_list.close();
     
     cout << "代码行数:" << sum_code << endl;
     system("pause");
 
     return 0;
 }

二、批量删除文件中的某一行

今天做的修改时有原因的,因为我正在将http://www.cplusplus.com/reference/做成一个chm文件(供离线查寻的文档)。通过Teleport Ultra软件获取到网站的代码后,代码有好多是404页面和用户登录页面。这些文件我是通过手工删除的,删除404页面就是根据文件大小来删除的(不会傻到对着一千多个文件一个一个打开查看,毕竟404页面大小是一样的)。删除登录页面是根据文件名的。文件名有new.cgi?或者edit.cgi?等等,是按文件名排序删除的。

然后使用EasyCHM软件开始制作。制作后出现了问题,打开页面预览出现

我郁闷了,我就打开源代码,找到了有这么一行,打开每一文件都这样。

因此,我就想到之前写的代码统计的程序了,然后我就做了个微小的修改把这一行删除掉。在上面的55行的while中加入

if (t_str == "onPrint.print();") {   cout << t_str << endl;   continue; }

因为要修改源文件,我就通过一个临时文件来存储修改后的文件,文件修改完成后再将临时文件的内容复制到源文件中,复制文件的代码段:

        out_tmp.open("tmp",std::ios::in);
        code_file.open(str_line.c_str(),std::ios::out);
        char ch;
        while(code_file && out_tmp.get(ch))
        {
            code_file.put(ch);
        }
        code_file.close();
        out_tmp.close();
        cout << "修改" << str_line << "完成" << endl;

全部代码如下:

删除指定行的代码
 #include <iostream>
 #include <fstream>
 #include <cstdlib>
 
 using std::cout;
 using std::endl;
 using std::fstream;
 using std::string;
 
 int main(int argc, char **argv)
 {
     //创建文件名列表文件,若存在则清空文件
     fstream file_list("file_list.txt",std::ios::out);
     file_list.close();
 
     //写入文件名列表到file_list.txt
     system("dir /a /b >> file_list.txt");
     
     long sum_code = 0;
     fstream code_file;
     file_list.open("file_list.txt", std::ios::in);
     string str_line = "";
     string t_str = "";
     unsigned int loc = 0;//查找文件名中的"."
     string str_last = "";
     
     while (!file_list.eof())
     {
         getline(file_list,str_line);
         loc = str_line.find(".",0);
 //        cout << "loc = " << loc << endl;
         if (loc != string::npos)
         {
             str_last = str_line.substr(loc);
 //            cout << "str_last = " << str_last << endl;
         }
         else
         {
             continue;
         }
         
         if (str_last.compare(".htm") == 0)
         {
             code_file.open(str_line.c_str(),std::ios::in);
             cout << "文件名 : " << str_line << endl;
         }
         else
         {
             continue;
         }
         
         fstream out_tmp("tmp",std::ios::out);
         //读文件行数
         while (!code_file.eof())
         {
             getline(code_file,t_str);
             if (t_str == "onPrint.print();")
             {
                 cout << t_str << endl;
                 continue;
             }
             out_tmp << t_str << endl;
             sum_code++;
         }
         code_file.close();
         out_tmp.close();
         cout << endl;
         
         //复制回来
         out_tmp.open("tmp",std::ios::in);
         code_file.open(str_line.c_str(),std::ios::out);
         char ch;
         while(code_file && out_tmp.get(ch))
         {
             code_file.put(ch);
         }
         code_file.close();
         out_tmp.close();
         cout << "修改" << str_line << "完成" << endl;
     }
     file_list.close();
     
     cout << "代码行数:" << sum_code << endl;
     system("pause");
 
     return 0;
 }
 
 

C++文件的批处理(2)

时间:2012-07-23 09:43 点击:45 次 【    

三、批量删除文件中的某一代码段

这样对所有文件批处理后又有了新问题,弹出的脚本对话框和上面的类似。这样慢慢删岂不会累死,我能不能把所有的<script></<script>都删除掉呢?我就先手动删除一个文件的所有<script> ... </<script>。然后放到EasyCHM软件中,结果真的好了,不弹出脚本错误对话框了(脚本都没了,何来脚本错误呢?)。这样我就放心了,因为脚本文件删除了不影响htm文件的界面。这样我就开始修改代码了,同样修改读文件那一段代码。

if (t_str.substr(0,7) == "<script")
            {
                while (!code_file.eof() 
                        && t_str != "</script>" 
                        && t_str.substr(t_str.size()>9?t_str.size()-9:0) != "</script>")
                {
                    cout << t_str << endl;
                    getline(code_file,t_str);
                }
                cout << t_str << endl;
                getline(code_file,t_str);
            }

这是放在读文件循环中的代码。每次读到一行的头几个字符是"<script"就对接下来的字符处理知道读 到"</script>"。因为我查了所有代码都类似,"<script"在每行的行首,"</script>"在字符串 的末尾后者行首,所以就只是简单的判断了下,没有考虑到一行中既有脚本文件又有非脚本文件的情况:

<script type="text/javascript">google_ad_height = 60;</script><div id="I_midclear"></div>
</div><script type="text/javascript"src="show_ads.js" tppabs="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

我想也不会有人会把这种代码写成一行吧。接着就测试了一个htm文件,结果可以了,只是有一个bug,运行一次后还残留着<script> ... </<script>。再运行一次就清理完了。先看有bug的代码:

有bug但是可以完成任务的代码
 #include <iostream>
 #include <fstream>
 #include <cstdlib>
 
 using std::cout;
 using std::endl;
 using std::fstream;
 using std::string;
 
 int main(int argc, char **argv)
 {
     //创建文件名列表文件,若存在则清空文件
     fstream file_list("file_list.txt",std::ios::out);
     file_list.close();
 
     //写入文件名列表到file_list.txt
     system("dir /a /b >> file_list.txt");
     
     long sum_code = 0;
     fstream code_file;
     file_list.open("file_list.txt", std::ios::in);
     string str_line = "";
     string t_str = "";
     unsigned int loc = 0;//查找文件名中的"."
     string str_last = "";
     
     while (!file_list.eof())
     {
         getline(file_list,str_line);
         loc = str_line.find(".",0);
 //        cout << "loc = " << loc << endl;
         if (loc != string::npos)
         {
             str_last = str_line.substr(loc);
 //            cout << "str_last = " << str_last << endl;
         }
         else
         {
             continue;
         }
         
         if (str_last.compare(".htm") == 0)
         {
             code_file.open(str_line.c_str(),std::ios::in);
             cout << "文件名 : " << str_line << endl;
         }
         else
         {
             continue;
         }
         
         fstream out_tmp("tmp",std::ios::out);
         //读文件行数
         while (!code_file.eof())
         {
             getline(code_file,t_str);
             if (t_str.substr(0,7) == "<script")
             {
                 while (!code_file.eof() 
                         && t_str != "</script>" 
                         && t_str.substr(t_str.size()>9?t_str.size()-9:0) != "</script>")
                 {
                     cout << t_str << endl;
                     getline(code_file,t_str);
                 }
                 cout << t_str << endl;
                 getline(code_file,t_str);
             }
             out_tmp << t_str << endl;
             sum_code++;
         }
         code_file.close();
         out_tmp.close();
         cout << endl;
         
         //复制回来
         out_tmp.open("tmp",std::ios::in);
         code_file.open(str_line.c_str(),std::ios::out);
         char ch;
         while(code_file && out_tmp.get(ch))
         {
             code_file.put(ch);
         }
         code_file.close();
         out_tmp.close();
         cout << "修改" << str_line << "完成" << endl;
     }
     file_list.close();
     
     cout << "代码行数:" << sum_code << endl;
     system("pause");
 
     return 0;
 }

接下来就是查找bug了,然后在解决bug,记住哦,做这些操作记得备份之前原版的htm文件哦,要不然你弄完后原版的文件没了怎么来找bug,怎么测试啊。关键还是看这里(循环读文件人后再判断的代码段):

fstream out_tmp("tmp",std::ios::out);
         //读文件行数
         while (!code_file.eof())
         {
             getline(code_file,t_str);
             if (t_str.substr(0,7) == "<script")
             {
                 while (!code_file.eof() 
                         && t_str != "</script>" 
                         && t_str.substr(t_str.size()>9?t_str.size()-9:0) != "</script>")
                 {
                     cout << t_str << endl;
                     getline(code_file,t_str);
                 }
                 cout << t_str << endl;
                 getline(code_file,t_str);
             }
             out_tmp << t_str << endl;
             sum_code++;
         }
         code_file.close();
         out_tmp.close();
         cout << endl;

要修改这段代码,需要来查看结果的,我把运行一次后的结果和原始文件对比了下,结果如下:

发现了问题,是因为执行了一次删除脚本后,接下来的脚本紧接在其下一行,读取的时候跳过了这一次检查,这样简单啊,只执行一次判定<script>,我把if改成while就可以了,修改后的代码如下:

最后代码
 #include <iostream>
 #include <fstream>
 #include <cstdlib>
 
 using std::cout;
 using std::endl;
 using std::fstream;
 using std::string;
 
 int main(int argc, char **argv)
 {
     //创建文件名列表文件,若存在则清空文件
     fstream file_list("file_list.txt",std::ios::out);
     file_list.close();
 
     //写入文件名列表到file_list.txt
     system("dir /a /b >> file_list.txt");
     
     long sum_code = 0;
     fstream code_file;
     file_list.open("file_list.txt", std::ios::in);
     string str_line = "";
     string t_str = "";
     unsigned int loc = 0;//查找文件名中的"."
     string str_last = "";
     
     while (!file_list.eof())
     {
         getline(file_list,str_line);
         loc = str_line.find(".",0);
 //        cout << "loc = " << loc << endl;
         if (loc != string::npos)
         {
             str_last = str_line.substr(loc);
 //            cout << "str_last = " << str_last << endl;
         }
         else
         {
             continue;
         }
         
         if (str_last.compare(".htm") == 0)
         {
             code_file.open(str_line.c_str(),std::ios::in);
             cout << "文件名 : " << str_line << endl;
         }
         else
         {
             continue;
         }
         
         fstream out_tmp("tmp",std::ios::out);
         //读文件行数
         while (!code_file.eof())
         {
             getline(code_file,t_str);
             while (t_str.substr(0,7) == "<script")
             {
                 while (!code_file.eof() 
                         && t_str != "</script>" 
                         && t_str.substr(t_str.size()>9?t_str.size()-9:0) != "</script>")
                 {
                     cout << t_str << endl;
                     getline(code_file,t_str);
                 }
                 cout << t_str << endl;
                 getline(code_file,t_str);
                     
             }
             out_tmp << t_str << endl;
             sum_code++;
         }
         code_file.close();
         out_tmp.close();
         cout << endl;
         
         //复制回来
         out_tmp.open("tmp",std::ios::in);
         code_file.open(str_line.c_str(),std::ios::out);
         char ch;
         while(code_file && out_tmp.get(ch))
         {
             code_file.put(ch);
         }
         code_file.close();
         out_tmp.close();
         cout << "修改" << str_line << "完成" << endl;
     }
     file_list.close();
     
     cout << "代码行数:" << sum_code << endl;
     system("pause");
 
     return 0;
 }

最后,我也完成了http://www.cplusplus.com/reference/离线chm版本。大家玩这个注意备份文件哦!!!


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值