我正在用C++编写程序。我试图获取程序可执行文件所在文件夹中的所有文件,并将它们存储在一个向量中。我被告知下面的代码应该可以工作,但FindFirstFile操作只能找到一个文件(它应该搜索的文件夹的名称)。我如何更改代码,以便它正确地查看文件夹?如何获取目录中的所有文件名?
std::vector<char*> fileArray;
//Get location of program executable
HMODULE hModule = GetModuleHandleW(NULL);
WCHAR path[MAX_PATH];
GetModuleFileNameW(hModule, path, MAX_PATH);
//Remove the executable file name from 'path' so that it refers to the folder instead
PathCchRemoveFileSpec(path, sizeof(path));
//This code should find the first file in the executable folder, but it fails
//Instead, it obtains the name of the folder that it is searching
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile(path, &ffd);
do
{
//The name of the folder is pushed onto the array because of the previous code's mistake
//e.g. If the folder is "C:\\MyFolder", it stores "MyFolder"
fileArray.push_back(ffd.cFileName); //Disclaimer: This line of code won't add the file name properly (I'll get to fixing it later), but that's not relevant to the question
} while (FindNextFile(hFind, &ffd) != 0); //This line fails to find anymore files
试图把\\ *在路径结束 – immibis