0

我想创建一个读取目录并在数据结构中查找并保存所有目录以及每个目录的所有子目录的方法。具体来说,我希望将名称引用为:

文件夹1 文件夹11 文件夹12 文件夹2 文件夹21 文件夹22 文件夹23

我显然需要一个递归函数。我在控制台应用程序中使用临时类成员方法:

private:
 struct dirent *ep;


void DirectoryReader::parseDirectory(char* readingDirectory)
{
char* tempDirectory = (char*)malloc(sizeof(readingDirectory) + 200); 
string temp = readingDirectory;

DIR *dp;
dp = opendir (readingDirectory);
   if (dp != NULL)
     {
       while (ep = readdir (dp))
       {
         puts (ep->d_name);

         temp += readingDirectory;
         temp += "/";
         temp += ep->d_name;

         char * buffer = new char[temp.length()];
         strcpy(buffer,temp.c_str());
         parseDirectory(buffer);
       }
         (void) closedir (dp);
     }
   else
     perror ("Couldn't open the directory");

}

好的,我不知道最好的书面代码,但我想首先了解捕获的名称。但是它不能正常工作,因为它不止一次考虑父目录的名称。即使我把同一个方法的调用(不执行递归),前两个名称是 . 和 .. 。为什么?

您能否建议我执行所述过程的功能或指出此功能所需的更正?

4

2 回答 2

4

正如一些评论指出的那样, boost::filesystem 是通常的方法。但是从你的代码看来你还是 C++ 的新手,所以我用 C++ 惯用的方式重写了,添加了一些有用的显示。高温高压

#include <dirent.h>
#include <iostream>
#include <string>
using namespace std;

struct DirectoryReader
{
    static void parseDirectory(string readingDirectory, int level);
};

void DirectoryReader::parseDirectory(string readingDirectory, int level)
{
    if (DIR *dp = opendir(readingDirectory.c_str()))
    {
        cout << string(level, ' ') << readingDirectory << endl;
        while (struct dirent *ep = readdir(dp))
            if (ep->d_type == DT_DIR && ep->d_name[0] != '.')
                parseDirectory(readingDirectory + "/" + ep->d_name, level + 1);
        closedir(dp);
    }
    else
        cerr << "Couldn't open the directory " << readingDirectory << endl;
}

int main_parsedir(int argc, char **argv)
{
    if (argc > 1)
        DirectoryReader::parseDirectory(argv[1], 0);
    return 0;
}

请注意,在您的代码中,存储struct dirent *ep;在类中是完全错误的,因为您在递归时会覆盖它......

于 2012-06-19T09:06:49.523 回答
2

目录枚举函数通常返回当前目录 (.) 和父目录 (..) 链接。你需要忽略它们。此外,您需要检查 ep->d_type。我为您编写了以下函数。请检查这是否按预期工作。我没有在输出中使用空格分隔符,因为各个文件夹中可以包含空格字符。另外,我不确定您将如何使用此输出,因为它会丢失树结构。

#include "dirent.h"

const string theDelimiter = "\\";

class DirectoryReader
{
public:
    string DirectoryReader::parseDirectory(string readingDirectory, const string& aCurrentFolderName)
    {
        string aChildren;
        aChildren += theDelimiter;
        aChildren += aCurrentFolderName;

        DIR *dp = opendir (readingDirectory.c_str());
        if (dp != NULL)
        {
            struct dirent *ep = NULL;
            while ((ep = readdir (dp)) && ep->d_type == DT_DIR )
            {
                string aDirName = ep->d_name;

                //  Ignore current directory and the parent directory links
                if(!aDirName.compare(".") || !aDirName.compare(".."))
                    continue;

                //  Form the full directory path
                string aFullFolderName = readingDirectory;
                aFullFolderName += string("\\");
                aFullFolderName += aDirName;

                string aChildrenFolders = parseDirectory(aFullFolderName, aDirName);
                if(aChildrenFolders.compare(""))
                {
                    aChildren += aChildrenFolders;
                }
            }

            (void) closedir (dp);
        }
        else
            perror ("Couldn't open the directory");

        return aChildren;
    }
};
于 2012-06-19T09:08:21.343 回答