C++遍历文件夹,查找出图像文件

[cpp:showcolumns]  view plain copy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. /* 
  2. ************************** 
  3. 版权所有: 东南大学 王楚楠 
  4. 当前版本: 1.0 
  5. 完成日期:2011年4月13日 
  6. ************************** 
  7. */  
  8.   
  9. #include<iostream.h>  
  10. #include<afx.h>  
  11. #include<string.h>  
  12. #include<windows.h>  
  13. #include<stdio.h>  
  14. #include<fstream.h>  
  15. void FindPic(CString IpPath,CString pic);//定义一个图像文件查找函数  
  16. FILE *stream;  
  17.   
  18. //主函数  
  19. void main()  
  20. {  
  21.     char root[100];//root为文件夹的路径  
  22.     cout<<"请输入根目录路径:"<<endl;  
  23.     gets(root);  
  24.     cout<<endl;  
  25.     CString Root=root;  
  26.     CString ss("//");  
  27.     //判断输入的路径是否以/结尾,如果不是,自动添加上  
  28.     if(Root.Right(1)!=ss)  
  29.         Root=Root+ss;  
  30.     stream=fopen("图像路径.txt","w");//打开txt文件  
  31.     //下面循环的作用是分别查找不同格式的图像文件  
  32.     for(int i=0;i<4;i++)  
  33.     {  
  34.         switch(i)  
  35.         {  
  36.         case 0: FindPic(Root,"jpg");  
  37.             break;  
  38.         case 1: FindPic(Root,"bmp");  
  39.             break;  
  40.         case 2: FindPic(Root,"png");  
  41.             break;  
  42.         case 3: FindPic(Root,"gif");  
  43.             break;  
  44.         }  
  45.     }  
  46.     fclose(stream);//关闭txt文件  
  47.     cout<<endl;  
  48.     cout<<"遍历结束,文件路径已写入txt"<<endl;  
  49.       
  50. }  
  51.   
  52. //以下是图像文件查找函数  
  53. void FindPic(CString IpPath,CString pic)  
  54. {  
  55.   
  56.     CFileFind tempFind;  
  57.     BOOL IsFinded=tempFind.FindFile(IpPath+"//*.*");  
  58.     while(IsFinded)  
  59.     {  
  60.         IsFinded=tempFind.FindNextFile();  
  61.         if(tempFind.IsDots())  
  62.             continue;//文件名称为"."或".."不合适  
  63.         CString tempName=tempFind.GetFileName();//获取文件名  
  64.   
  65.         if(tempFind.IsDirectory())//如果是子文件夹,递归  
  66.         {  
  67.             FindPic(IpPath+tempName+"//",pic);  
  68.         }  
  69.         else  
  70.         {  
  71.             //通过文件后缀判断文件是否是需要的格式  
  72.             CString extend;  
  73.             extend=tempName.Right(tempName.GetLength()-tempName.ReverseFind('.')-1);  
  74.             if(extend==pic)//如果后缀符合,则保存路径  
  75.             {  
  76.             cout<<tempName<<endl;  
  77.             //路径写入到txt  
  78.             fwrite(IpPath,sizeof(char),IpPath.GetLength(),stream);  
  79.             fwrite(tempName,sizeof(char),tempName.GetLength(),stream);  
  80.             fwrite("/n",sizeof(char),1,stream);  
  81.             }  
  82.         }  
  83.     }  
  84.     tempFind.Close();  
  85. }  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值