在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)
说明:
程序使用 io.h 中的 _findfirst 和 _findnext 函数遍历文件夹,故而程序只能在 Windows 下使用。
程序遍历当前文件夹,对其中的文件夹执行递归遍历。同时检查遍历到的文件是否属于指定类型,如果是,则将在该文件中查找指定字符串。
在文件中查找字符串时,开辟一个与指定字符串 text (长度为len )同样大小的字符串数组 temp 。数组上有两个指针:一个是字符串比较的开始位置 s ,一个是新字符写入的位置 d 。每从文件中读入一个字符,就写入 temp[d] ,之后 temp 从 s 到 d 与 text 从 0 到 len-1 比较,之后, s 与 d 均后移一位,再继续读入字符,写入,比较,后移。。。
代码:
- #include<stdio.h>
- #include<vector>
- #include<string.h>
- #include<io.h>
- using namespace std;
- vector<char*> types;
- char text[];
- void ls_path(char * path);
- bool is_in_types(char* filename);
- void findtext(char * filename,char* text);
- void solve(char* name,struct _finddata_t *f) {
- if(strcmp(f->name,".")==)return ;
- if(strcmp(f->name,"..")==)return ;
- char *fullpath=new char[];
- strcpy(fullpath,name);
- int len=strlen(name);
- fullpath[len-]='\0';
- strcat(fullpath,f->name);
- if(f->attrib&_A_SUBDIR) {
- strcat(fullpath,"/*");
- ls_path(fullpath);
- } else {
- if(is_in_types(f->name)) {
- findtext(fullpath,text);
- }
- }
- delete fullpath;
- }
- void ls_path(char * path) {
- struct _finddata_t f;
- int p;
- char *name=new char[];
- strcpy(name,path);
- if((p=_findfirst(name, &f))!=-) {
- solve(name,&f);
- while(_findnext(p, &f)==) {
- solve(name,&f);
- }
- }
- delete name;
- }
- int strrncmp(char* a,const char* b,int n) {//比较两字符串的最后n个字符
- int len=strlen(a);
- int j=;
- for(int i=len-n; i<=len-; i++) {
- if(a[i]!=b[j])return false;
- j++;
- }
- return j==n?true:false;
- }
- bool is_in_types(char* filename) {
- for(int i=; i<types.size(); i++) {
- if(strrncmp(filename,types[i],strlen(types[i]))) {
- return true;
- }
- }
- return false;
- }
- bool cmp(const char* temp,const int len,const int s,const int d,const char* text) {
- int j=;
- for(int i=s;; i++,i%=len) {
- if(temp[i]!=text[j])return false;
- if(i==d)break;
- j++;
- }
- return true;
- }
- void findtext(char * filename,char* text) {
- FILE *f=fopen(filename,"r");
- char c;
- int linenum=;
- int len=strlen(text);
- char* temp=new char[len];
- int s=,d=len-;
- while(c=fgetc(f),c!=EOF) {
- temp[d]=c;
- if(cmp(temp,len,s,d,text))printf("文件名: %s \n行号: %d\n",filename,linenum+);
- if(c=='\n'||c=='\r'||c=='\r\n') {
- linenum++;
- }
- d++;
- d%=len;
- s++;
- s%=len;
- }
- delete temp;
- fclose(f);
- }
- int main() {
- printf("**************************************\n");
- printf("本程序在其所在文件夹中查找指定类型文件\n中是否有指定字符串,并输出所在行号。\n");
- printf(" CopyRight: maxuewei2\n");
- printf("**************************************\n");
- while(true) {
- types.clear();
- printf("\n请输入要查找的字符串:\n");
- while(gets(text),strcmp(text,"")==);
- printf("请输入文件类型,如‘txt’:(按两下ENTER开始查找)\n");
- char t[];
- while(gets(t),strcmp(t,"")!=) {
- char* tt=new char[];
- strcpy(tt,".");
- strcat(tt,t);
- types.push_back(tt);
- }
- delete t;
- printf("查找结果:\n");
- ls_path("*");
- }
- types.clear();
- delete text;
- getchar();
- return ;
- }
运行效果:
程序完成于2016.4.15
博客更新于2016.4.15
END
在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)的更多相关文章
- linux下删除3分钟之前指定文件夹下的指定类型文件
如果想要修改crontab,一般做以下的几步就可以了 将crontab 推到一个自定义的文件上 crontab -l>;tmp 编辑这个文件,做需要的修改 vi tmp 推回crontab cr ...
- 怎样cp文件夹时忽略指定的文件夹和文件
在备份ltedecoder程序时,须要把此文件夹拷由到bak文件夹下.但decoder文件夹下有个大文件,不须要备份,还有日志问题,也不须要备份,怎样实现呢?? 方法: cd /source-dir ...
- C#读取文件夹下所有指定类型,并返回相应类型数据
C#读取文件夹下所有文件 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心 ...
- C#遍历文件夹下面所有指定格式文件
C#遍历指定文件夹中的所有文件 DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);//遍历文件夹foreach(DirectoryIn ...
- matlab操作之--读取指定文件夹下的“指定格式”文件
%% 正负样本所在folder fext='*.png';%要读取的文件格式 positiveFolder='F:\课题\Crater detection\machingLearning\Positi ...
- Windows删除文件夹下的指定格式文件(递归删除)
问题描述: 今天遇到一个需求,需要对文件夹进行文件筛选.目录结构较为复杂(目录较多,层次较深),数据量较大(总共60GB左右). 鉴于上述情况,直接排除了人工处理方式(否则小伙伴们会打死我的). 解决 ...
- iOS 获得指定文件夹下的指定格式文件
这个容易忘记,然后只能用些自己写的长代码代替了....这里做个备忘 主要用到NSFileManager的 contentsOfDirectoryAtPath:error: 和 NSArray的 pat ...
- C#项目打开/保存文件夹/指定类型文件,获取路径
C#项目打开/保存文件夹/指定类型文件,获取路径 转:http://q1q2q363.xiaoxiang.blog.163.com/blog/static/1106963682011722424325 ...
- [转]C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件
原文:http://www.crifan.com/csharp_call_explorer_to_open_destinate_folder_and_select_specific_file/ C#中 ...
随机推荐
- Mysql在大型网站的应用架构演变
原创文章,转载请注明: 转载自http://www.cnblogs.com/Creator/本文链接地址: Mysql在大型网站的应用架构演变 本文已经被多处转载,包括CSDN推荐以及码农周刊等等,阅 ...
- 排列组和在c语言中的应用
排列组和在c中很常见,但是这个排列组和是通过循环来实现的,和数学中的还是有一点区别的,而且c中的方法也不尽相同,而且我遇到c中的数学问题总会纠结于数学上是怎么实现的但是我自己又不会,所以就没了兴趣,例 ...
- 数据结构:链表(python版)续:带有尾节点引用的单链表
#!/usr/bin/env python # -*- coding:utf-8 -*- from chapter3.single_linked_list import LNode,LinkedLis ...
- MongoDB基础入门002--基本操作,增删改查
一.这里只是演示最基本的操作,更多的信息可以去官网.https://docs.mongodb.com/manual 打开一个cmd,输入mongo命令打开shell,其实这个shell就是mongod ...
- linux下tomcat安全配置
转:http://www.tuicool.com/articles/R7fQNfQ 0x00 删除默认目录 安装完tomcat后,删除 $CATALINA_HOME/webapps 下默认的所有目录文 ...
- Semaphore(计数信号量)
//对象池public class Pool<T> { private int size; private List<T> items = new ArrayList<T ...
- 【转载】实用的Javascript获取网页屏幕可见区域高度
本文转载原地址:这里 document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 docu ...
- jQuery $(document).ready()和JavaScript onload事件
jQuery $(document).ready()和JavaScript onload事件 Why we need a right time? 对元素的操作和事件的绑定需要等待一个合适的时机,可以看 ...
- Objective-C Runtime 运行时之四:Method Swizzling
理解Method Swizzling是学习runtime机制的一个很好的机会.在此不多做整理,仅翻译由Mattt Thompson发表于nshipster的Method Swizzling一文. Me ...
- The operation couldn’t be completed. (LaunchServicesError error 0.)
问题描述: 当运行Xcode时,编译代码成功,但是登陆模拟器失败,显示错误:The Operation couldn't be completed.(LaunchServicesError error ...