c++列出(LINUX)中的所有目录和子目录
c++ list all directories and subdirectories within in (LINUX )
我是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[]
}
请注意,如果条目不包含任何实际目录,函数就不会调用自己?这一点很重要,因为这就是避免无限递归的方法。请注意,您可能希望这样做,以便可以取消此操作,更大的文件系统这些日子可能需要很多时间来处理。我通常的做法是另起炉灶线程并在后台进行搜索,并让后台线程检查取消标志,以防用户想要停止操作,从而可以发布剩余时间、完成百分比等信息。它有点粗糙,但是这应该会让任何对这类事情感兴趣的人朝着正确的方向发展。和请记住始终正确检查错误并进行异常处理,这是我看到新程序员总是跳过。
- 递归列出所有目录中的C++与Python与Ruby的性能
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 我怎样才能将所有子目录与 cmake 自动匹配
- 如何在CPP的给定目录中列出UTF编码的文件名?
- 使文件夹中的所有文件可供所有项目(子目录)访问
- 如何删除列出的"QGraphicsPathItem"对象以控制进程内存使用情况?
- 从所有列出的文件和子目录中更改目录
- 使用类在C++中存储和列出变量/方法是否是一种好的做法
- 如何在组合框中列出所有可用的 VCL 样式,并在组合框更改事件中应用该样式C++生成器?
- 子目录之间的 CMAKE 共享变量
- 有没有办法列出所有共享内存对象的名称?
- CMake:为什么added_subdirectory对其他子目录不可见
- Qt C++11 字符串通过子字符串比较列出交集
- 使用C 列出目录中的文件,而不是递归,只有文件和无子目录
- 一种使用 boost 和 c++ 列出目录和子目录中所有文件的方法
- 用于列出子目录中所有文件的递归功能
- 使用 QDir 的过滤器列出子目录中的文件
- c++列出(LINUX)中的所有目录和子目录
- 有效地列出目录中的所有子目录