程序使用 io.h 中的 _findfirst 和 _findnext 函數遍歷文件夾,故而程序只能在 Windows 下使用。
程序遍歷當前文件夾,對其中的文件夾執行遞歸遍歷。同時檢查遍歷到的文件是否屬於指定類型,如果是,則將在該文件中查找指定字符串。
在文件中查找字符串時,開辟一個與指定字符串 text (長度為len )同樣大小的字符串數組 temp 。數組上有兩個指針:一個是字符串比較的開始位置 s ,一個是新字符寫入的位置 d 。每從文件中讀入一個字符,就寫入 temp[d] ,之後 temp 從 s 到 d 與 text 從 0 到 len-1 比較,之後, s 與 d 均後移一位,再繼續讀入字符,寫入,比較,後移。。。
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