在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)

说明:

  程序使用 io.h 中的 _findfirst 和 _findnext 函数遍历文件夹,故而程序只能在 Windows 下使用。  

  程序遍历当前文件夹,对其中的文件夹执行递归遍历同时检查遍历到的文件是否属于指定类型,如果是,则将在该文件中查找指定字符串。 

  在文件中查找字符串时,开辟一个与指定字符串 text 长度为len 同样大小的字符串数组 temp 数组上有两个指针:一个是字符串比较的开始位置 ,一个是新字符写入的位置 。每从文件中读入一个字符,就写入 temp[d] ,之后 temp 从 到 与 text 从  len-1 比较,之后, 与 均后移一位,再继续读入字符,写入,比较,后移。。。

代码:

  1 #include<stdio.h>
  2 #include<vector>
  3 #include<string.h>
  4 #include<io.h>
  5 using namespace std;
  6 
  7 vector<char*> types;
  8 char text[256];
  9 
 10 void ls_path(char * path);
 11 bool is_in_types(char* filename);
 12 void findtext(char * filename,char* text);
 13 
 14 void solve(char* name,struct _finddata_t *f) {
 15     if(strcmp(f->name,".")==0)return ;
 16     if(strcmp(f->name,"..")==0)return ;
 17     char *fullpath=new char[256];
 18     strcpy(fullpath,name);
 19     int len=strlen(name);
 20     fullpath[len-1]='\0';
 21     strcat(fullpath,f->name);
 22     if(f->attrib&_A_SUBDIR) {
 23         strcat(fullpath,"/*");
 24         ls_path(fullpath);
 25     } else {
 26         if(is_in_types(f->name)) {
 27             findtext(fullpath,text);
 28         }
 29     }
 30     delete fullpath;
 31 }
 32 void ls_path(char * path) {
 33     struct _finddata_t f;
 34     int p;
 35     char *name=new char[260];
 36     strcpy(name,path);
 37     if((p=_findfirst(name, &f))!=-1) {
 38         solve(name,&f);
 39         while(_findnext(p, &f)==0) {
 40             solve(name,&f);
 41         }
 42     }
 43     delete name;
 44 }
 45 int strrncmp(char* a,const char* b,int n) {//比较两字符串的最后n个字符
 46     int len=strlen(a);
 47     int j=0;
 48     for(int i=len-n; i<=len-1; i++) {
 49         if(a[i]!=b[j])return false;
 50         j++;
 51     }
 52     return j==n?true:false;
 53 }
 54 bool is_in_types(char* filename) {
 55     for(int i=0; i<types.size(); i++) {
 56         if(strrncmp(filename,types[i],strlen(types[i]))) {
 57             return true;
 58         }
 59     }
 60     return false;
 61 }
 62 bool cmp(const char* temp,const int len,const int s,const int d,const char* text) {
 63     int j=0;
 64     for(int i=s;; i++,i%=len) {
 65         if(temp[i]!=text[j])return false;
 66         if(i==d)break;
 67         j++;
 68     }
 69     return true;
 70 }
 71 void findtext(char * filename,char* text) {
 72     FILE *f=fopen(filename,"r");
 73     char c;
 74     int linenum=0;
 75     int len=strlen(text);
 76     char* temp=new char[len];
 77     int s=0,d=len-1;
 78     while(c=fgetc(f),c!=EOF) {
 79         temp[d]=c;
 80         if(cmp(temp,len,s,d,text))printf("文件名: %s \n行号: %d\n",filename,linenum+1);
 81         if(c=='\n'||c=='\r'||c=='\r\n') {
 82             linenum++;
 83         }
 84         d++;
 85         d%=len;
 86         s++;
 87         s%=len;
 88     }
 89     delete temp;
 90     fclose(f);
 91 }
 92 int main() {
 93     printf("**************************************\n");
 94     printf("本程序在其所在文件夹中查找指定类型文件\n中是否有指定字符串,并输出所在行号。\n");
 95     printf("              CopyRight:  maxuewei2\n");
 96     printf("**************************************\n");
 97     while(true) {
 98         types.clear();
 99         printf("\n请输入要查找的字符串:\n");
100         while(gets(text),strcmp(text,"")==0);
101         printf("请输入文件类型,如‘txt’:(按两下ENTER开始查找)\n");
102         char t[30];
103         while(gets(t),strcmp(t,"")!=0) {
104             char* tt=new char[30];
105             strcpy(tt,".");
106             strcat(tt,t);
107             types.push_back(tt);
108         }
109         delete t;
110         printf("查找结果:\n");
111         ls_path("*");
112     }
113     types.clear();
114     delete text;
115     getchar();
116     return 0;
117 }

 运行效果:

程序完成于2016.4.15

博客更新于2016.4.15

END

原文地址:https://www.cnblogs.com/maxuewei2/p/5395621.html