linux dirent.h 实现getFiles

    1. void getFiles(std::string root_dir, std::string ext, std::vector<std::string>& files) {
    2. DIR *dir;
    3. struct dirent *ptr;
    4. if ((dir = opendir(root_dir.c_str())) == NULL) {
    5. gLogInfo << "Open dir error..." << std::endl;
    6. return;
    7. }
    8. while ((ptr = readdir(dir)) != NULL) {
    9. if (strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0) {
    10. continue;
    11. } else if(ptr->d_type == 8) {// file
    12. char* dot = strchr(ptr->d_name, '.');
    13. if (dot && !strcasecmp(dot, ext.c_str())) {
    14. std::string filename(root_dir);
    15. filename.append("/").append(ptr->d_name);
    16. files.push_back(filename);
    17. }
    18. } else if(ptr->d_type == 10) { // link file
    19. continue;
    20. } else if(ptr->d_type == 4) {// dir
    21. std::string dir_path(root_dir);
    22. dir_path.append("/").append(ptr->d_name);
    23. getFiles(dir_path.c_str(), ext, files);
    24. }
    25. }
    26. closedir(dir);
    27. }
    std::vector<std::string> filenames;
        filenames.reserve(20480);
        for (size_t i = 0; i < onnx_args.dataDirs.size(); i++) {
            getFiles(onnx_args.dataDirs[i], ".jpg", filenames);
        }
    

    参考