(转)C++文件的批处理——批量文件增删改查

原博客地址:http://www.cnblogs.com/hanxi/archive/2012/07/22/2603584.html

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

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

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

include

include

include

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

include

include

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;

}

三、批量删除文件中的某一代码段
  这样对所有文件批处理后又有了新问题,弹出的脚本对话框和上面的类似。这样慢慢删岂不会累死,我能不能把所有的

include

include

include

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;

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

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

fstream out_tmp(“tmp”,std::ios::out);
//读文件行数
while (!code_file.eof())
{
getline(code_file,t_str);
if (t_str.substr(0,7) == “

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值