c++ - 如何从根文件夹及其所有子文件夹生成目录树?

标签 c++ tree directory dirent.h

好的,所以我正在尝试获取文件夹和子文件夹的目录,但它只会进入无限循环。创建文件夹和子文件夹目录的更好方法是什么?因为我真的不知道。

到目前为止,这是我的代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

struct Directory{
  public:
        int indent;
        vector<string> files;
        vector<string> folders;
        string dir;
        Directory(string mydir){ dir = mydir;}
};

int getfolders (string dir, vector<string> &folders)
{
    DIR *dp;
    struct stat st;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        stat(dirp->d_name, &st);

        if(S_ISDIR(st.st_mode)){
            if(dirp->d_name[0] != '.')
                folders.push_back(string(dirp->d_name));
        }
    }
    closedir(dp);
    return 0;
}

/*function... might want it in some class?*/
int getfiles (string dir, vector<string> &files)
{

    DIR *dp;
    struct stat st;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        stat(dirp->d_name, &st);

        if(!S_ISDIR(st.st_mode)){
            files.push_back(string(dirp->d_name));
        }
    }
    closedir(dp);
    return 0;
}

int main()
{
    struct Directory root = Directory(".");


    vector<string> display = vector<string>();

    cout << "hello\n";

    getfiles(root.dir,root.files);
    getfolders(root.dir,root.folders);

    cout << "hello\n";

    vector<Directory> dirs = vector<Directory>();

    for(int i = 0; i < (int) root.folders.size(); i++){
        dirs.push_back(Directory(root.folders[i]));

       getfiles(dirs[i].dir,dirs[i].files);
        getfolders(dirs[i].dir,dirs[i].folders);


        //infinate loop start
        for(int j = 0; j < (int) dirs[i].folders.size(); j++){
            dirs.push_back(Directory(dirs[i].folders[j]));

            getfiles(dirs[i].dir,dirs[i].files);
            getfolders(dirs[i].dir,dirs[i].folders);
        }
        //infinate loop end
    }

    cout << "hello\n";

    for (int i = 0; i < (int) root.folders.size();i++) {
        cout << root.folders[i] << endl;
        for(int j = 0; j < (int) dirs[i].folders.size(); j++){
            cout << dirs[i].folders[j] << endl;
        }
    }
    return 0;
}

最佳答案

我认为您必须以不同方式处理指向目录的符号链接(symbolic link)。可能有无限循环的来源:

说/tmp/foo 是/tmp 的符号链接(symbolic link),那么我认为你的程序会在 .==/tmp 时进入死循环

关于c++ - 如何从根文件夹及其所有子文件夹生成目录树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3847074/

相关文章:

c++ - 通过 C++ 中的构造函数将地址参数传递给基类指针

c++ - 防止不必要的 C++ 仿函数对象拷贝

c++ - Linux Ubuntu 14.04, boost 库和线程编译

c++ - 使用指针表示法从矩阵读取数据

java - 如何在 BST 中测试我的删除方法

java - 如何将 child 添加到 n 数组树中的特定节点?

java - toString 导致 StackOverflow 错误

java - 我们如何在单个语句中使用 RandomAccessFile 创建文件夹和文件?

Python:获取给定文件夹中特定文件类型的名称

python - 文件夹名称带有\n