卡住了使用dirent.h在子文件夹C ++中查找所有文件

时间:2018-07-18 18:30:38

标签: c++ dirent.h

我目前在使用dirent.h扩展名。目的是为该功能提供一个从其开始的路径目录。然后,该函数将浏览该目录中的所有子文件夹并在其中查找文件。直到在同一目录中有两个文件夹,所有这些工作。然后,程序会顽固地选择一个文件夹,而忽略另一个文件夹。 我的代码乱七八糟。 (该问题在代码底部被注释)

    #include <iostream>
    #include "dirent.h"
    #include <windows.h>

    DWORD getPathType(const char *path); //checks if path leads to folder or file
    const char *dirlist[20];  //contains all directories (not files, only dir's)
    int dirAm = 0;            //specifies amount of directories

    void loadTextures(const char *loaddir) {

        dirlist[dirAm] = loaddir;  //specifies starting directory
        dirAm++;

        for(int i = 0; i<20; i++) {
            DIR *dir;
            struct dirent *ent;
            const char *currentdir = dirlist[i];  //stores current directory
            dir = opendir(currentdir);            //opens current directory

            if (dir != NULL) {
                std::cout << "[OPENING dir]\t" << currentdir << std::endl;

                while ((ent = readdir(dir)) != NULL) {
                    const char *filename;  //stores current file/folder name
                    char fullDirName[100]; //stores full path name (current dir+file name, for example /images/+image1.png)
                    DWORD filetype;        //checking path type (file/folder)
                    filename = ent->d_name; //gets current file name

                    strcpy(fullDirName, currentdir); //concats current directory and file name to get full path, for example /images/image1.png
                    strcat(fullDirName, filename);

                    filetype = getPathType(fullDirName); //gets path type
                    if (filetype == FILE_ATTRIBUTE_DIRECTORY) {
                        //if its a directory add it to the list of directories, dirlist, the naming process is the same as above
                        const char *filenameIn;
                        char fullDirNameIn[100];

                        filenameIn = ent->d_name;

                        strcpy(fullDirNameIn, currentdir);
                        strcat(fullDirNameIn, filenameIn);
                        strcat(fullDirNameIn, "/");
                        std::cout << "[FOUND dir]\t" << fullDirNameIn<<std::endl;

                        dirlist[dirAm] = fullDirNameIn;
                        dirAm++;

                        /* Here is the problem! The cout line above finds all
 folders in a directory and saves them in the array, but as soon as the new for
 loop iteration starts, the values in the dirlist array... change? And I have no
 idea what is going on */

                    } else {
                        std::cout << "[FOUND file]\t" << fullDirName << std::endl;
                    }
                }
            }
        }

这是getPathType()函数。我猜很简单。

DWORD getPathType(const char *path) {
    DWORD fileat;
    fileat = GetFileAttributesA(path);
    return fileat;
}

最后,这是控制台输出:

[OPENING dir]   img/                 <- opens starting dir
[FOUND dir]     img/lvl0/            <- finds lvl0, should store it in dirlist
[FOUND dir]     img/lvl1/            <- finds lvl1
[OPENING dir]   img/lvl1/
[FOUND file]    img/lvl1/player2.png
[OPENING dir]   img/lvl1/            <- only opens lvl1
[FOUND file]    img/lvl1/player2.png

我知道这是一个很大的问题,但是如果有人可以分享想法,我将非常感激。

1 个答案:

答案 0 :(得分:1)

您将忽略char数组的范围。实际上,您正在这样做

const char *dirlist[20];

while (...)
{
    char fullDirNameIn[100];
    ....
    dirlist[dirAm] = fullDirNameIn;
}

问题是您的数组的作用域为while循环的主体,但是您将指针存储在while循环之外。退出循环主体后(即,当您进行迭代时),数组的内容将变得不确定,但仍然有指向它的指针。

解决方案很简单,这是一个值得学习的教训。 不要使用指针,请执行经验丰富的程序员的操作,而改为使用std::string

std::string dirlist[20];

while (...)
{
    std::string fullDirNameIn;
    ....
    dirlist[dirAm] = fullDirNameIn;
}