释放双眼,带上耳机,听听看~!
下面是一段用于遍历文件夹内的指定类型文件的程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 1// C++遍历文件夹
2// Author:www.icvpr.com
3// Blog: http://blog.csdn.net/icvpr
4
5#include <iostream>
6#include <string>
7#include <io.h>
8
9using namespace std;
10
11int main(int argc, char** argv)
12{
13 string fileFolderPath = "..\\myFileFolder";
14 string fileExtension = "jpg";
15
16 string fileFolder = fileFolderPath + "\\*." + fileExtension;
17
18
19 // 遍历文件夹
20 char fileName[2000];
21
22 struct _finddata_t fileInfo; // 文件信息结构体
23
24 // 1. 第一次查找
25 long findResult = _findfirst(fileFolder.c_str(), &fileInfo);
26 if (findResult == -1)
27 {
28 _findclose(findResult);
29 return -1;
30 }
31
32 // 2. 循环查找
33 do
34 {
35 sprintf(fileName, "%s\\%s", fileFolderPath.c_str(), fileInfo.name);
36
37 if ( fileInfo.attrib == _A_ARCH) // 是存档类型文件
38 {
39 cout<<"fileName: "<<fileName<<endl;
40 }
41
42 } while (!_findnext(findResult, &fileInfo));
43
44
45 _findclose(findResult);
46
47 return 0;
48}
49