forked from controny/LogoReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_read.cpp
107 lines (92 loc) · 2.84 KB
/
linux_read.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
# include <vector>
#ifdef linux
#include <unistd.h>
#include <dirent.h>
#endif
using namespace std;
/**
* @function: 获取cate_dir目录下的所有文件名
* @param: cate_dir - string类型
* @result:vector<string>类型
*/
vector<string> getFiles(string cate_dir)
{
vector<string> files;//存放文件名
#ifdef linux
DIR *dir;
struct dirent *ptr;
char base[1000];
string p;
if ((dir=opendir(cate_dir.c_str())) == NULL)
{
perror("Open dir error...");
exit(1);
}
while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) ///current dir OR parrent dir
continue;
else if(ptr->d_type == 8) ///file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
files.push_back(ptr->d_name);
//cout << ptr->d_name << endl;
else if(ptr->d_type == 10) ///link file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
continue;
else if(ptr->d_type == 4) ///dir
{
//files.push_back(p.assign(cate_dir).append("/").append(ptr->d_name));
files.push_back(ptr->d_name);
/*
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_nSame);
readFileList(base);
*/
}
}
closedir(dir);
#endif
//排序,按从小到大排序
sort(files.begin(), files.end());
return files;
}
vector<vector<string> > getAllFiles(string cate_dir) {
vector<string> subdir = getFiles(cate_dir);
cout << subdir.size() <<endl;
vector<vector<string> > total(51, vector<string>());
string p;
vector<string> temp;
for (int i = 0; i < 51; ++i) {
temp = getFiles(p.assign(cate_dir).append("/").append(subdir[i]));
for (int j = 0; j < temp.size(); ++j) {
temp[j] = p.assign(cate_dir).append("/").append(subdir[i]).append("/").append(temp[j]);
}
total[i] = temp;
cout << i << " " << p.assign(cate_dir).append("/").append(subdir[i]) << endl;
}
return total;
}
int main(void)
{
DIR *dir;
string basePath = "/root/Desktop/LogoReader/data/CarLogos51";
cout << basePath << endl;
cout<<endl<<endl;
vector<vector<string> > files=getAllFiles(basePath);
for (int i=0; i<files.size(); i++)
{
// cout << i << endl;
for (int j = 0; j < files[i].size(); ++j) {
cout << files[i][j] << " ";
}
cout << endl;
}
return 0;
}