(转)C++文件的批处理——批量文件增删改查
2015-07-21 10:07
621 查看
原博客地址:/article/4976710.html
一、统计代码行数
几个星期前做了个统计代码的小程序,对文件的批处理需要获取文件列表,因为是在windows下操作的,所以百度到一个系统调用
system(“dir /a /b >> file_list.txt”);
功能就是把当前目录下的所有文件名写到file_list.txt文件中去。这样我就得到了所有的文件名,从而就可以对文件批处理了。对每一个文件的操作就是首先读取文件名,然后就打开文件,接着一行一行的读文件,没读取一行文件就累计代码行数。过程很简单,代码如下:
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();
// cout << “loc = ” << loc << endl;
if (loc != string::npos)
{
str_last = str_line.substr(loc);
// cout << “str_last = ” << str_last << endl;
}
else
{
continue;
}
}
二、批量删除文件中的某一行
今天做的修改时有原因的,因为我正在将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;
全部代码如下:
删除指定行的代码
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();
// cout << “loc = ” << loc << endl;
if (loc != string::npos)
{
str_last = str_line.substr(loc);
// cout << “str_last = ” << str_last << endl;
}
else
{
continue;
}
}
三、批量删除文件中的某一代码段
这样对所有文件批处理后又有了新问题,弹出的脚本对话框和上面的类似。这样慢慢删岂不会累死,我能不能把所有的
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();
// cout << “loc = ” << loc << endl;
if (loc != string::npos)
{
str_last = str_line.substr(loc);
// cout << “str_last = ” << str_last << endl;
}
else
{
continue;
}
}
要修改这段代码,需要来查看结果的,我把运行一次后的结果和原始文件对比了下,结果如下:
发现了问题,是因为执行了一次删除脚本后,接下来的脚本紧接在其下一行,读取的时候跳过了这一次检查,这样简单啊,只执行一次判定,我把if改成while就可以了,修改后的代码如下:
fstream out_tmp(“tmp”,std::ios::out);
//读文件行数
while (!code_file.eof())
{
getline(code_file,t_str);
if (t_str.substr(0,7) == “
一、统计代码行数
几个星期前做了个统计代码的小程序,对文件的批处理需要获取文件列表,因为是在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) == “
相关文章推荐
- MFC中C++类如何封装API
- C++学习笔记碎屑--结构体与构造函数
- C++中static关键字的总结
- 黑马程序员_C语言基础_关键字、printf函数、scanf函数
- VC++ 多线程模拟鼠标点击
- (转)C++中虚函数功能的实现机制
- C++多态性
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20) C语言
- PAT (Basic Level) Practise (中文)1002. 写出这个数 (20) C语言
- PAT (Basic Level) Practise (中文)1001. 害死人不偿命的(3n+1)猜想 (15) C语言
- log4cxx配置使用(二)
- PAT (Basic Level) Practise (中文)1020. 月饼 (25) C语言
- YTU-OJ-Problem I: 熟悉题型——类设计( 矩形类定义【C++】)
- 线性链表的创建 C语言
- PAT (Basic Level) Practise (中文)1022. D进制的A+B (20) C语言
- YTU-OJ-Problem D: C++习题 虚函数-计算图形面积
- YTU-OJ-Problem C: C++习题 抽象基类
- [leetcode-236]Lowest Common Ancestor of a Binary Tree(C语言)236
- 2015.7.21 c语言程序设计 第二堂
- C++基本变量类型