gpt4 book ai didi

c++ - 提取所有文件名 - FindFirstFile

转载 作者:可可西里 更新时间:2023-11-01 11:57:54 25 4
gpt4 key购买 nike

我想获取特定目录中所有文件的列表。为此 |我使用以下代码

void GetFileListing(std::list<std::string>& listing, std::string directory, std::string fileFilter, bool recursively=true)
{


//check if directory exits:

DWORD attribs = ::GetFileAttributesA(directory.c_str());
if (attribs == INVALID_FILE_ATTRIBUTES || !(attribs & FILE_ATTRIBUTE_DIRECTORY)) {
return ;
}

// If we are going to recurse over all the subdirectories, first of all
// get all the files that are in this directory that match the filter
if (recursively)
GetFileListing(listing, directory, fileFilter, false);

directory += "\\";

WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;

// Setup the filter according to whether we are getting the directories
// or just the files
std::string filter = directory + (recursively ? "*" : fileFilter);

// Find the first file in the directory.
hFind = FindFirstFile(LPCWSTR(filter.c_str()), &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
DWORD dwError = GetLastError();
if (dwError!=ERROR_FILE_NOT_FOUND)
{
std::cout << "Invalid file handle for filter "<<filter<<". Error is " << GetLastError() << std::endl;
}
}
else
{
// Add the first file found to the list
if (!recursively)
{
wstring wFindFileData = FindFileData.cFileName;
listing.push_back(directory + std::string(wFindFileData.begin(),wFindFileData.end()));
}

// List all the other files in the directory.
while (FindNextFile(hFind, &FindFileData) != 0)
{
if (!recursively)
{
wstring wFindFileData = FindFileData.cFileName;
listing.push_back(directory + std::string(wFindFileData.begin(),wFindFileData.end()));
}
else
{
// If we found a directory then recurse into it
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0 && FindFileData.cFileName[0]!='.')
{
wstring wFindFileData = FindFileData.cFileName;
GetFileListing(listing, directory + std::string(wFindFileData.begin(),wFindFileData.end()), fileFilter);
}
}
}

DWORD dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
std::cout << "FindNextFile error. Error is "<< dwError << std::endl;
}
}
}

int main(int argc, char *argv[])
{
if(argc < 2)
return 0;
std::list<std::string> listing;
GetFileListing(listing, argv[1], "*");
for(std::list<std::string>::iterator it = listing.begin(); it!=listing.end();++it)
{
std::cout << *it << std::endl;
}
}

在 args 中,我转移了一个有效的现有目录。实际上 args[1] 变成了“C:\dir”。但我没有得到所需的列表,而是出现以下错误:

Invalid file handle for filter C:\dir\*. Error is 123

我不明白这里有什么问题?

最佳答案

您将 ANSI 与 UNICODE 混合使用,甚至更糟,将 char* 转换为至 wchar_t* .

std::string filter;
// ...
LPCWSTR(filter.c_str()) // this does not work and causes the error!

如果您需要您的函数使用 std::string , 将其完全保留为 ANSI(使用 FindFirstFileAFindNextFileAWIN32_FIND_DATAA )。否则使用 std::wstring而是使用以 W 结尾的函数/结构而不是 A .

Windows header 定义 FindFirstFile作为FindFirstFileW如果UNICODE被定义并且FindFirstFileA除此以外。因此,对于通用解决方案,typedef a std::basic_string<TCHAR>并将其用作字符串类型。这样它在 UNICODE 时使用 std::wstring已定义,否则为 std::string。

关于c++ - 提取所有文件名 - FindFirstFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13483986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com