gpt4 book ai didi

c++ Windows API递归搜索未返回预期目录

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:16:17 25 4
gpt4 key购买 nike

我目前正在尝试编写一个程序,使用 windows.h 中的实用程序递归搜索文件。当我执行以下代码时,文件路径不断添加到搜索的最后一个文件路径中,但未搜索文件夹并且句柄未标记为无效。我已经为此苦恼了几天。为什么这不是递归搜索?编辑:更正代码以使用按位比较。发生相同的错误。

    #include "stdafx.h"
#include <iostream>
#include <windows.h> // Microsoft Windows’ main library.
#include <tchar.h> // Needed for _TEXT macro.
#include "Strsafe.h" // Microsoft's library for secure strings.

using namespace std;
typedef wchar_t* LPWSTR, *PWSTR;
int layer = 0;

int recursionFindAbsraction(LPWSTR Dir, LPWSTR FilNam, LPWSTR filePath);
//original directory, new directory with *.* attached
//new directory with entire directory, new entire directory with *.* attached
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t holder[MAX_PATH];//using this instead of string
wchar_t direct[MAX_PATH];
wchar_t filePath[MAX_PATH];

wcout << "Please enter the directory you wish to search: " << endl;
wcin >> direct;
wcout << direct << endl;


wcout << "Please enter the filename (program will automatically seach for all files like it): " << endl;
wcin >> holder;
wcout << holder << endl;

recursionFindAbsraction(direct, holder, filePath);
getchar();
getchar();
return 0;

}

int recursionFindAbsraction(LPWSTR Dir, LPWSTR FilNam, LPWSTR filePath){
WIN32_FIND_DATAW ptrFileData;
HANDLE hFile = NULL;
BOOL bGetNext = true;

wchar_t newDir[MAX_PATH];
wchar_t newDir2[MAX_PATH];
wchar_t filePathHolder[MAX_PATH];

//add slash then put directory into new variable, newDir
StringCchCatW(Dir, MAX_PATH, _TEXT("\\"));

//Here, we split the path to avoid appending *.* to the current directory
StringCchCopyW(filePathHolder, MAX_PATH, Dir);
StringCchCopyW(newDir, MAX_PATH, Dir);
StringCchCopyW(newDir2, MAX_PATH, Dir);
StringCchCatW(newDir2, MAX_PATH, _TEXT("*.*"));
hFile = FindFirstFile(newDir2, &ptrFileData);

if (hFile == INVALID_HANDLE_VALUE)
{
printf("FindFirstFile failed (%d)\n", GetLastError());
getchar();
//return 0;
}

while (bGetNext){

if ((ptrFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)
{
int setLoop = (ptrFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
wcout << " file Attribute bitwise: " << setLoop;
wcout << "file hidden " << endl;
int counter = 0; counter++;
wcout << "counter: " << counter << endl;
wcout << "string compare: " << _wcsicmp(ptrFileData.cFileName, _TEXT("..")) << endl;
wcout << "filename: " << ptrFileData.cFileName << endl;
Sleep(100);
/*
if (_wcsicmp(ptrFileData.cFileName, _TEXT(".")) == 0){
wcout << "Breaking1. " << endl;
continue;
}
if (_wcsicmp(ptrFileData.cFileName, _TEXT("..")) == 0){
wcout << "Breaking2. " << endl;
continue;
}*/
}

else
{
if (_wcsicmp(ptrFileData.cFileName, FilNam) == 0){
wcout << "The first file found is: " << ptrFileData.cFileName << endl;
//_tprintf(TEXT("The first file found is %s\n"), ptrFileData.cFileName);
FindClose(hFile);
//getchar();
break;
}
if ((((ptrFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) && ((ptrFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == 0))
{//must check to see if folder
layer++;
wcout << "Layer: " << layer << endl;
ptrFileData.cFileName;
//when this gets called because it's a folder, the name
//gets added to the wrong directory
StringCchCatW(newDir, MAX_PATH, ptrFileData.cFileName);
wcout << " newDir/fulldir: " << newDir<< endl;
//resolves full path name at this point
wcout << "filePathHolder from last else: " << filePathHolder << endl;
wcout << "filename: " << ptrFileData.cFileName << endl;

recursionFindAbsraction(newDir, FilNam, filePath);

}

}
wcout << "&ptrFileData: " << &ptrFileData << endl;
wcout << "hFile: " << hFile << endl;
bGetNext = FindNextFile(hFile, &ptrFileData);
wcout << " exit: " << bGetNext<< endl;
}
FindClose(hFile);
return 0;
}

最佳答案

这是一个递归搜索的简单例子。请注意,它使用 do{...}while(...); 这使得跳到循环末尾变得更容易

void findfile_recursive(const std::wstring &folder, const std::wstring &filename, std::vector<std::wstring> &files)
{
std::wstring wildcard = folder + L"\\*";
WIN32_FIND_DATA fd;
HANDLE handle = FindFirstFile(wildcard.c_str(), &fd);
if (handle == INVALID_HANDLE_VALUE)
return;
do
{
if (wcscmp(fd.cFileName, L".") == 0 || wcscmp(fd.cFileName, L"..") == 0)
continue;
std::wstring path = folder + L"\\" + std::wstring(fd.cFileName);

if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
findfile_recursive(path, filename, files);
else if (_wcsicmp(fd.cFileName, filename.c_str()) == 0)
files.push_back(path);

} while (FindNextFile(handle, &fd));
FindClose(handle);
}

int _tmain(int, wchar_t*[])
{
std::vector<std::wstring> files;
findfile_recursive(L"c:\\test", L"file.txt", files);
for (auto file : files)
std::wcout << file << endl;
}

您可以更改函数以使用 do/while。递归调用函数时,确保它有正确的参数。 newDir 循环中的变化,你不能将它作为引用重用。请改用 Dir

int recursionFindAbsraction(LPWSTR Dir, LPWSTR FilNam, LPWSTR filePath)
{
...
//while (bGetNext)***
do
{
if (wcscmp(ptrFileData.cFileName, L".") == 0 || wcscmp(ptrFileData.cFileName, L"..") == 0)
continue;

if (ptrFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
continue;//***skip hidden files and directories

if (ptrFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//***wrong redirection:
//StringCchCatW(newDir, MAX_PATH, ptrFileData.cFileName);

//***change to:
StringCchCopyW(newDir, MAX_PATH, Dir);
StringCchCatW(newDir, MAX_PATH, ptrFileData.cFileName);
recursionFindAbsraction(newDir, FilNam, filePath);
}
else if (_wcsicmp(ptrFileData.cFileName, FilNam) == 0)
{
std::wcout << "The first file found is: " << Dir << ptrFileData.cFileName << endl;
}

} while (FindNextFile(hFile, &ptrFileData));//***

FindClose(hFile);
return 0;
}

关于c++ Windows API递归搜索未返回预期目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33226628/

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