卡在使用dirent.h查找子文件夹C++中的所有文件
Stuck using dirent.h to find all files in subfolders C++
我目前一直在使用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
我知道这是一个很大的问题,但如果有人能分享这方面的想法,我将不胜感激。
您忽略了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;
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到