首页 > 编程 > C++ > 正文

C语言按关键字搜索文件夹中文件的方法

2020-05-23 14:18:00
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了C语言按关键字搜索文件夹中文件的方法,涉及C语言文件操作及字符串查找的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C语言按关键字搜索文件夹中文件的方法。分享给大家供大家参考。具体实现方法如下:

方法1:

 

 
  1. #include<iostream> 
  2. #include<string> 
  3. #include<io.h> 
  4. using namespace std; 
  5. void filesearch(string path,string mode) 
  6. { 
  7. struct _finddata_t filefind; 
  8. if(path[path.size()-1]=='//') 
  9. path.resize(path.size()-1); 
  10. string curr=path+"//*.*"; 
  11. int done=0,handle; 
  12. if((handle=_findfirst(curr.c_str(),&filefind))==-1) 
  13. return; 
  14. while(!(done=_findnext(handle,&filefind))) 
  15. { 
  16. if(!strcmp(filefind.name,"..")) 
  17. continue; 
  18. curr=path+"//"+filefind.name; 
  19. if(strstr(filefind.name,mode.c_str())) 
  20. cout<<curr<<endl; 
  21. if (_A_SUBDIR==filefind.attrib) 
  22. filesearch(curr,mode); 
  23. }  
  24. _findclose(handle);  
  25. } 
  26. void main() 
  27. { 
  28. string path,mode; 
  29. cout<<"请输入要搜的目录"<<endl; 
  30. cin>>path; 
  31. cout<<"请输出包含字符"<<endl; 
  32. cin>>mode; 
  33. filesearch(path,mode); 
  34. } 

方法2:

 

 
  1. #include<stdio.h> 
  2. #include<string.h> 
  3. #include<dir.h> 
  4. #define SIZE 12 
  5. void find_creat_file(char *); 
  6. int main(void) 
  7. { 
  8. char filename[SIZE]; 
  9. strcpy(filename,"filename"); 
  10. find_creat_file(filename); 
  11. getch(); 
  12. return 0; 
  13. } 
  14. void find_creat_file(char *filename) 
  15. { 
  16. struct ffblk ffblk; 
  17. int done; 
  18. done=findfirst("d://filename",&ffblk,0); 
  19. if(done==0) 
  20. return 0; 
  21. else 
  22. mkdir("d://filename"); 
  23. } 

希望本文所述对大家的C语言程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表