我正在用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