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
| #include <iostream> #include <sys/stat.h> #include <dirent.h> #include <string.h> #include <unistd.h> #include <queue> #include <string>
std::vector<std::string> listDirAndFiles(const std::string& dir) { DIR * d = NULL; struct dirent * entry = NULL;
std::vector<std::string> flist; std::queue<std::string> queue;
queue.push(dir);
while(!queue.empty()) { auto path = queue.front(); queue.pop(); d = opendir(path.c_str()); if(d == NULL) { std::cout << "failed to open: " << path << std::endl; continue; }
entry = readdir(d);
for(; entry != NULL; entry = readdir(d)) { if(entry->d_type == DT_DIR) { if(strcmp(entry->d_name, ".") ==0) continue; if(strcmp(entry->d_name, "..") ==0) continue; queue.push(path + "/" + entry->d_name); }
flist.push_back(path + "/" + entry->d_name); }
closedir(d); }
return flist; }
int main(int argc, char** argv) { if(argc != 2) return 1;
auto list = listDirAndFiles(argv[1]); std::cout << "find dir and files: " << list.size() << std::endl; return 0; }
|