-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam.cpp
52 lines (41 loc) · 1.14 KB
/
exam.cpp
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
50
51
52
#include <iostream>
#include <string>
#include <dirent.h>
#include <vector>
#include <memory.h>
using namespace std;
// 读取文件夹下所有文件的路径
vector<string> getFileList(string dir)
{
DIR *pathname = opendir(dir.c_str());
if (pathname == NULL) {
cout << "opendir failed" << endl;
}
vector<string> allPath;
struct dirent *entry;
while ((entry = readdir(pathname)) != NULL) {
// dir
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
string dirNew = dir + "/" + entry->d_name;
vector<string> tempPath = getFileList(dirNew);
allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
} else {
string name = entry->d_name;
string newDir = dir + "/" + name;
allPath.push_back(newDir);
}
}
closedir(pathname);
return allPath;
}
int main()
{
string dir = "/home/wpb/test";
vector<string> str = getFileList(dir);
for (auto& v : str) {
cout << v << endl;
}
}