微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Dirent.h 获取目录的所有子目录

如何解决使用 Dirent.h 获取目录的所有子目录

我正在尝试获取父目录的所有子目录。我知道使用 boost::filesystem 或(在 c++17 中)std::fileystem 会容易得多,但是我在设置 boost 时遇到了问题,我不想为 c++17 获得 VS(我' m 使用 MinGW)。

以下是我的代码的关键部分:

#include<dirent.h>
#include<deque>
#include<iostream>
#include<cstring>

struct Folder {
    DIR *directory;
    char* path;
};

std::deque<Folder> get_folder(char* path) { 
    std::deque<Folder> folders;
    
    DIR *dir;
    struct dirent *entry;

    bool first_time = true;
    struct Folder first_one = {};
    bool occurences_of_first = 0;
    bool folder_has_subdirs = false;
    char* full_path;
    std::string string_for_c_str;
    
    for(int i = -1; i < (int) folders.size(); i++) {
        
        if (i == -1 and folders.size() == 0) {
            i++;
        }
        
        dir = opendir(path);
        
        std::cout << "Iteration: " << i << std::endl;
        
        while(dir) {
            entry = readdir(dir);
            string_for_c_str = std::string(path) + std::string("\\") + std::string(entry->d_name);
            full_path = (char*) string_for_c_str.c_str();
            
            if (not (is_folder(full_path) or entry->d_name == "." or entry->d_name == "..")) {
                continue;
            }
            
            std::cout << "Fullpath: " << full_path << std::endl;
            std::cout << "Path: " << path << std::endl;
            
            Folder directory = {opendir(full_path),full_path};
            folders.push_back(directory);
            folder_has_subdirs = true;
        }
        
        std::cout << " Check Point 1" << std::endl;
        if (first_time == true) {
            first_one = folders.front();
        }
        std::cout << " Check Point 2" << std::endl;
        if (first_time == false) {
            if (folders.front() == first_one) {
                if (occurences_of_first == 1) {
                    return folders;
                }
                else {
                    occurences_of_first = 1;
                }
            }
        }
        std::cout << " Check Point 3" << std::endl;
        if (folder_has_subdirs == true) {
            folders.push_back(folders.front());
            
        }
        std::cout << " Check Point 4" << std::endl;
        first_time = false;
    }
    return folders;
}

int main() {
    char path[] = "C:\\MinGW";
    std::cout << "Results: " << get_folder(path).size() << std::endl;
    
    return 0;
}

输出

Iteration: 0
Fullpath: C:\MinGW\.
Path: C:\MinGW
Fullpath: C:\MinGW\..
Path: C:\MinGW
Fullpath: C:\MinGW\bin
Path: C:\MinGW
Fullpath: C:\MinGW\include
Path: C:\MinGW
Fullpath: C:\MinGW\lib
Path: C:\MinGW
Fullpath: C:\MinGW\libexec
Path: C:\MinGW
Fullpath: C:\MinGW\mingw32
Path: C:\MinGW
Fullpath: C:\MinGW\msys
Path: C:\MinGW
Fullpath: C:\MinGW\share
Path: C:\MinGW
Fullpath: C:\MinGW\var
Path: C:\MinGW

如您所见,程序在第一个“检查点”之前没有错误地崩溃,但它到达了 while 循环的末尾。这个程序出了什么问题,我该如何解决

提前致谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。