簡體   English   中英

通過C ++中的遞歸查找目錄和子目錄

[英]Find directories and sub-directories through recursion in C++

我想創建一種方法,該方法讀取目錄,並在其中包含所有目錄以及每個目錄的所有子目錄的數據結構中查找並保存。 具體來說,我希望這些名稱被引用為:

folder1文件夾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");

}

好的,我不知道最好的書面代碼,但是我最初希望查看捕獲的名稱。 但是,由於它會多次考慮父目錄的名稱,因此無法正常工作。 即使我退出了相同方法的調用(不執行遞歸),前兩個名稱也是。 和.. 為什么?

您能建議我執行描述的過程的功能還是指示對此功能進行的校正?

正如一些評論所指出的那樣,通常使用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; 在課堂上是完全錯誤的,因為您在遞歸時將其覆蓋...

目錄枚舉功能通常返回當前目錄(。)和父目錄(..)鏈接。 您需要忽略它們。 另外,您需要檢查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;
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM