- /*
- **************************
- 版权所有: 东南大学 王楚楠
- 当前版本: 1.0
- 完成日期:2011年4月13日
- **************************
- */
- #include<iostream.h>
- #include<afx.h>
- #include<string.h>
- #include<windows.h>
- #include<stdio.h>
- #include<fstream.h>
- void FindPic(CString IpPath,CString pic);//定义一个图像文件查找函数
- FILE *stream;
- //主函数
- void main()
- {
- char root[100];//root为文件夹的路径
- cout<<"请输入根目录路径:"<<endl;
- gets(root);
- cout<<endl;
- CString Root=root;
- CString ss("//");
- //判断输入的路径是否以/结尾,如果不是,自动添加上
- if(Root.Right(1)!=ss)
- Root=Root+ss;
- stream=fopen("图像路径.txt","w");//打开txt文件
- //下面循环的作用是分别查找不同格式的图像文件
- for(int i=0;i<4;i++)
- {
- switch(i)
- {
- case 0: FindPic(Root,"jpg");
- break;
- case 1: FindPic(Root,"bmp");
- break;
- case 2: FindPic(Root,"png");
- break;
- case 3: FindPic(Root,"gif");
- break;
- }
- }
- fclose(stream);//关闭txt文件
- cout<<endl;
- cout<<"遍历结束,文件路径已写入txt"<<endl;
- }
- //以下是图像文件查找函数
- void FindPic(CString IpPath,CString pic)
- {
- CFileFind tempFind;
- BOOL IsFinded=tempFind.FindFile(IpPath+"//*.*");
- while(IsFinded)
- {
- IsFinded=tempFind.FindNextFile();
- if(tempFind.IsDots())
- continue;//文件名称为"."或".."不合适
- CString tempName=tempFind.GetFileName();//获取文件名
- if(tempFind.IsDirectory())//如果是子文件夹,递归
- {
- FindPic(IpPath+tempName+"//",pic);
- }
- else
- {
- //通过文件后缀判断文件是否是需要的格式
- CString extend;
- extend=tempName.Right(tempName.GetLength()-tempName.ReverseFind('.')-1);
- if(extend==pic)//如果后缀符合,则保存路径
- {
- cout<<tempName<<endl;
- //路径写入到txt
- fwrite(IpPath,sizeof(char),IpPath.GetLength(),stream);
- fwrite(tempName,sizeof(char),tempName.GetLength(),stream);
- fwrite("/n",sizeof(char),1,stream);
- }
- }
- }
- tempFind.Close();
- }
C++遍历文件夹,查找出图像文件
最新推荐文章于 2023-05-18 22:11:00 发布