我有一个根文件夹包括
+ Test1(folder) > Test2(folder)>Test3(folder)>file1,file2....(file)
+ Test5(folder) > Test6(folder)>file1,file2....(file)
如何通过输入获取列表文件是 C++ 中的根文件夹。如果列表文件(文件1,文件2 ....)是子根文件夹(根文件夹>文件1,文件2 ...),我成功获得列表文件。但就我而言,要获取列表文件,我必须扫描许多父文件夹。在我的情况下如何获取列表文件?这是我的代码,用于在没有父文件夹的情况下获取列表文件
DIR *dirStr = NULL;
dirStr = opendir(rootpath)
dirent *nextFile = NULL;
while ((nextFile = readdir(dirStr))!=NULL)
{
// Avoid hidden files
//Scan all file an dictionary
if (nextFile->d_name[0] != '.')
{
cout<<nextFile->d_name<<endl;
}
}
最佳答案
您将使用递归。
递归函数是调用自身的函数。还有相互递归的函数,它们调用另一个函数,而另一个函数又再次调用调用函数。
递归的一个典型例子是这个朴素的阶乘函数:
int factorial(int x) {
if (x == 1) return 1; // assume 1
else if (x > 1) return x*factorial(x-1);
else throw std::logic_error("factorial called with argument<1");
}
关于c++ - 在C++中通过dirent.h获取根文件夹中的列表文件夹和列表文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15071143/