【发布时间】:2013-04-01 21:48:38
【问题描述】:
我正在尝试递归获取目录的所有文件和子文件夹。这是我目前所拥有的。
#include <iostream>
#include "dirent.h"
#include <io.h>
using namespace std;
void listDir(char *directory)
{
DIR *dir;
struct dirent *ent;
if((dir = opendir (directory)) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
if(strstr(ent->d_name,".") != NULL)
cout<<ent->d_name<<endl;
else
{
strcat(directory,ent->d_name);
strcat(directory,"\\");
strcat(directory,"\0");
cout<<directory<<endl;
cin.get();
listDir(directory);
}
}
}
closedir (dir);
}
int main(int param, char **args)
{
char *path = new char[];
path = args[1];
strcat(path, "\\");
listDir(path);
cin.get();
return 0;
}
我正在使用 dirent(实际上很酷,如果您还没有的话,请获取它)并且当我递归获取文件夹时,它似乎添加到我的子文件夹末尾的目录中。例如:
Downloads、Images 和 Includes 都是我的 Jakes625 文件夹的子文件夹。也许我错过了什么?
【问题讨论】:
-
你有什么问题?
-
这段代码甚至无法编译:这条语句需要一个大小:
char *path = new char[];你为什么不使用 c++ 字符串? -
为什么这个标签是 C 而不是 C++?您使用的是
#include <iostream>,所以它必须是 C++,而不是 C。 -
您的问题是什么?您想要程序做什么而不是它现在做什么?
-
递归列出每个子目录中的每个文件。