使用 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 (将#修改为@)