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

c++ list all directories and subdirectories within in (LINUX )

本文关键字:子目录 列出 LINUX c++      更新时间:2023-10-16

我是c++的新手。有人能给我一些如何在LINUX中递归地获取所有目录及其子目录的代码吗。我在互联网上没有找到任何可以帮助我的东西(或有效的代码)。我需要获取文件夹中的所有文件;的子文件夹。

在UBUNTU我没有getfiles,目录。。。

在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
}

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

#include <ftw.h>
int ftw(const char *dirpath,
        int (*fn) (const char *fpath, const struct stat *sb,
                   int typeflag),
        int nopenfd);

ftw()函数为给定树中的每个文件和目录调用提供的回调。

窗口:http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx

Unix/Linux:http://www.linuxquestions.org/questions/programming-9/c-list-files-in-directory-379323

递归地应用与应用于顶级目录的算法相同的算法。

除了Dmitri的答案外,您可能有兴趣使用nftw库函数,该函数"为您执行递归"

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

该页面还显示了一个示例。

列表目录上的答案只是答案的第一部分,但我没有看到任何人回答递归部分。要递归地做任何事情,你必须创建一个"调用自己"的子例程——注意,在处理符号链接时应该小心,尤其是在使用nfs时像/exports这样的情况下,这可能会导致循环递归并将你锁定在中篇循环中!基本上:

这不是真正的代码,它的伪代码试图帮助您获得更好的想法关于递归是如何工作的,而不会让你与语言混淆,这个想法可以可以应用于任何具有某种调用返回机制的语言这些天几乎是我能想到的每一种语言

// 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[]
}

请注意,如果条目不包含任何实际目录,函数就不会调用自己?这一点很重要,因为这就是避免无限递归的方法。请注意,您可能希望这样做,以便可以取消此操作,更大的文件系统这些日子可能需要很多时间来处理。我通常的做法是另起炉灶线程并在后台进行搜索,并让后台线程检查取消标志,以防用户想要停止操作,从而可以发布剩余时间、完成百分比等信息。它有点粗糙,但是这应该会让任何对这类事情感兴趣的人朝着正确的方向发展。和请记住始终正确检查错误并进行异常处理,这是我看到新程序员总是跳过。