C++列出中的所有目录和子目录(LINUX)

问题描述:

我是新的C++。有人可以请给我一些代码如何获得所有目录和所有它的子目录在LINUX中RECURSIVELY。我在互联网上没有找到任何可以帮助我的东西(或者可以工作的代码)。我需要使用文件夹和它的子文件夹来获取所有文件。C++列出中的所有目录和子目录(LINUX)

在Ubuntu我没有的GetFiles,目录...

另外的Dmitri's answer,你可能会感兴趣的使用nftw库函数“做递归你”

的答案上列出目录只是答案的第一部分,但我没有看到任何人回答递归部分。要递归地做任何事情你必须创建一个“调用自己”的子例程 - 注意在处理符号链接时应该小心,特别是在使用nfs时/ export等情况下,这可能导致循环递归并将你锁定在无限循环!基本上是:

这不是真正的代码,它的伪代码,试图帮助你的递归是如何工作的一个更好的想法 没有这个想法混淆你的语言可以 在具有某种语言的任何应用其中 这几天几乎所有的语言我能想到

// PSEUDO-CODE 
stiring entriesarray[] myfunc(string basedir) 
{ 
    string results[] = getfilesandfolders(basedir) // you might want to specify a filter 
    for each string entry in results 
    { 
     // if you want only files you'll need to test for if entry is a file 
     entriesarray.add(entry) 
     if (entry is a directory && entries is not a symbolic link) 
     { 
      string moreentriesarray[] = myfunc(entry) 
      entriesarray.join(moreentriesarray) 
     } 
    } 
    return entriesarray[] 
} 

通知如何,如果项目不包含任何实际的目录,该函数不调用自身调用返回的机制?这很重要,因为这是如何避免无限递归。不过要注意的是,您可能想要使其可以取消此操作,但现在较大的文件系统 可能需要很长时间才能处理。我通常这样做的方式是启动另一个 线程并在后台执行搜索,并让后台线程检查 取消标志,以防止用户想要停止操作,因此可以发布有关多少信息剩余时间,完成百分比等等。它有点粗糙,但这应该让任何人对这种类型的东西感兴趣,走向正确的方向。记得要始终正确地检查错误并进行异常处理,这是 ,我看到新的程序员跳过所有的时间。

试试这个在Linux上:

#include <iostream> 
#include <string> 
#include <dirent.h> 

void ProcessDirectory(std::string directory); 
void ProcessFile(std::string file); 
void ProcessEntity(struct dirent* entity); 

std::string path = "/path/to/directory/"; 

int main() 
{ 
    std::string directory = "theDirectoryYouWant"; 
    ProcessDirectory(directory);  

    return 0; 
} 

void ProcessDirectory(std::string directory) 
{ 
    std::string dirToOpen = path + directory; 
    auto dir = opendir(dirToOpen.c_str()); 

    //set the new path for the content of the directory 
    path = dirToOpen + "/"; 

    std::cout << "Process directory: " << dirToOpen.c_str() << std::endl; 

    if(NULL == dir) 
    { 
     std::cout << "could not open directory: " << dirToOpen.c_str() << std::endl; 
     return; 
    } 

    auto entity = readdir(dir); 

    while(entity != NULL) 
    { 
     ProcessEntity(entity); 
     entity = readdir(dir); 
    } 

    //we finished with the directory so remove it from the path 
    path.resize(path.length() - 1 - directory.length()); 
    closedir(dir); 
} 

void ProcessEntity(struct dirent* entity) 
{ 
    //find entity type 
    if(entity->d_type == DT_DIR) 
    {//it's an direcotry 
     //don't process the '..' and the '.' directories 
     if(entity->d_name[0] == '.') 
     { 
      return; 
     } 

     //it's an directory so process it 
     ProcessDirectory(std::string(entity->d_name)); 
     return; 
    } 

    if(entity->d_type == DT_REG) 
    {//regular file 
     ProcessFile(std::string(entity->d_name)); 
     return; 
    } 

    //there are some other types 
    //read here http://linux.die.net/man/3/readdir 
    std::cout << "Not a file or directory: " << entity->d_name << std::endl; 
} 

void ProcessFile(std::string file) 
{ 
    std::cout << "Process file  : " << fileToOpen.c_str() << std::endl; 

    //if you want to do something with the file add your code here 
} 

使用nftw。它提供了各种选项来微调目录遍历。

该页面还显示an example

递归是不必要的。 Linux上有一个工具可以迭代执行此操作。

#include <ftw.h> 

int ftw(const char *dirpath, 
     int (*fn) (const char *fpath, const struct stat *sb, 
        int typeflag), 
     int nopenfd); 

ftw()功能要求在给定的树中的每个文件和目录提供的回调函数。