C++ 使用扩展功能在整个系统中搜索文件
以下代码产生运行时错误: 窗口文件Search.exe中0x773315de处未处理的异常:0xC0000005:访问冲突 我不知道是什么引起的。你能指出我的错误吗 以下是可能包含罪魁祸首的函数:C++ 使用扩展功能在整个系统中搜索文件,c++,windows,winapi,filesystems,C++,Windows,Winapi,Filesystems,以下代码产生运行时错误: 窗口文件Search.exe中0x773315de处未处理的异常:0xC0000005:访问冲突 我不知道是什么引起的。你能指出我的错误吗 以下是可能包含罪魁祸首的函数: int fileSearcher::findFilesRecursivelly(const TCHAR* curDir,const TCHAR* fileName,bool caseSensitive,TCHAR* output) { HANDLE hFoundFile; WIN32_
int fileSearcher::findFilesRecursivelly(const TCHAR* curDir,const TCHAR* fileName,bool caseSensitive,TCHAR* output)
{
HANDLE hFoundFile;
WIN32_FIND_DATA foundFileData;
TCHAR nextDirBuffer[MAX_PATH]=TEXT("");
SetCurrentDirectory(curDir);
//Fetch inside current directory
hFoundFile = FindFirstFileEx(fileName,FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchNameMatch ,NULL , FIND_FIRST_EX_LARGE_FETCH);
if(hFoundFile != INVALID_HANDLE_VALUE)
{
do
{
nothingFound = false;
wcscat(output,curDir);
wcscat(output,TEXT("\\"));
wcscat(output,foundFileData.cFileName);
wcscat(output,TEXT("\n"));
}
while(FindNextFile(hFoundFile,&foundFileData));
}
//Go to the subdirs
hFoundFile = FindFirstFileEx(TEXT("*"),FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchLimitToDirectories ,NULL , FIND_FIRST_EX_LARGE_FETCH); //This line of code was on the call stack
if(hFoundFile != INVALID_HANDLE_VALUE)
{
do
{
wcscat(nextDirBuffer,curDir);
wcscat(nextDirBuffer,TEXT("\\"));
wcscat(nextDirBuffer,foundFileData.cFileName);
findFilesRecursivelly(nextDirBuffer,fileName,caseSensitive,outputBuffer);
}
while(FindNextFile(hFoundFile,&foundFileData));
}
return 0;
}
不太重要的代码:
文件搜索.h
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
namespace fileSearch
{
class fileSearcher
{
public:
fileSearcher();
void getAllPaths(const TCHAR* fileName,bool caseSensitive,TCHAR* output);
/*Returns all matching pathes at the current local system. Format:
[A-Z]:\[FirstPath\foo1...\fileName]
[A-Z]:\[SecondPath\foo2...\fileName]
[A-Z]:\[ThirdPath\foo3...\fileName]
...
[A-Z]:\[FourthPath\foo4...\fileName]
Also an asterisk sign is supported, as in regular expressions.
This functions uses WinApi methods.
*/
int findFilesRecursivelly(const TCHAR* curDir,const TCHAR* fileName,bool caseSensitive,TCHAR* output);
//Searches for the file in the current and in sub directories. NOT IN PARENT!!!!!!!!!!!!!!!!!!!!! Returns true if the file is found.
private:
static const int MAX_NUMBER_OF_FILES = 100;
static const int MAX_OUTPUT_SIZE = 2000;
bool nothingFound;
TCHAR outputBuffer[MAX_OUTPUT_SIZE];
};
}
#ifndef UNICODE
#定义UNICODE
#恩迪夫
#包括
名称空间文件搜索
{
类文件搜索器
{
公众:
filesearch();
void getAllPath(const TCHAR*文件名,bool区分大小写,TCHAR*输出);
/*返回当前本地系统上的所有匹配路径。格式:
[A-Z]:\[FirstPath\foo1…\fileName]
[A-Z]:\[SecondPath\foo2…\fileName]
[A-Z]:\[ThirdPath\foo3…\fileName]
...
[A-Z]:\[FourthPath\foo4…\fileName]
还支持星号符号,如在正则表达式中。
此函数使用WinApi方法。
*/
int findfiles递归(const TCHAR*curDir,const TCHAR*fileName,bool区分大小写,TCHAR*output);
//在当前目录和子目录中搜索该文件。不在父目录中!!!!!!!!!!!!!!!!!!!!!如果找到该文件,则返回true。
私人:
静态常量int最大文件数=100;
静态常量int MAX_OUTPUT_SIZE=2000;
一无所有;
TCHAR outputBuffer[最大输出大小];
};
}
。。。和其他文件each.cpp
#ifndef UNICODE
#define UNICODE
#endif
#include "File Search.h"
using namespace fileSearch;
fileSearcher::fileSearcher()
{
nothingFound = true;
}
void fileSearcher::getAllPaths(const TCHAR* fileName,bool caseSensitive, TCHAR* output)
{
TCHAR localDrives[50];
TCHAR currentDrive;
int voluminesChecked=0;
TCHAR searchedVolumine[5];
GetLogicalDriveStrings(sizeof(localDrives)/sizeof(TCHAR),localDrives);
//For all drives:
for(int i=0; i < sizeof(localDrives)/sizeof(TCHAR); i++)
{
if(localDrives[i] >= 65 && localDrives[i] <= 90)
{
currentDrive = localDrives[i];
voluminesChecked++;
}
else continue;
searchedVolumine[0] = currentDrive;
searchedVolumine[1] = L':';
searchedVolumine[2] = 0;
outputBuffer[0]=0;
findFilesRecursivelly(searchedVolumine,fileName,caseSensitive,outputBuffer);
(nothingFound) ? wcscpy(output,L"File not found") : wcscpy(output,outputBuffer);
}
}
#ifndef UNICODE
#定义UNICODE
#恩迪夫
#包括“文件搜索.h”
使用名称空间文件搜索;
fileSearcher::fileSearcher()
{
虚无=真实;
}
void filesearch::getAllPath(常量TCHAR*文件名,布尔区分大小写,TCHAR*输出)
{
TCHAR本地驱动器[50];
电流驱动;
int voluminesChecked=0;
TCHAR搜索体积[5];
GetLogicalDriveStrings(sizeof(localDrives)/sizeof(TCHAR),localDrives);
//对于所有驱动器:
对于(int i=0;i 如果(localDrives[i]>=65&&localDrives[i]看起来像是缓冲区溢出。在通过目录树递归时,您忘记了每个目录都包含对自身的引用(名称为“.”)和对其父目录的引用(名称为“..”),您必须从递归中排除它们。因此,请执行此操作
do
{
if (wcscmp(foundFileData.cFileName, TEXT(".") == 0 ||
wcscmp(foundFileData.cFileName, TEXT("..") == 0)
{
continue;
}
wcscat(nextDirBuffer,curDir);
wcscat(nextDirBuffer,TEXT("\\"));
wcscat(nextDirBuffer,foundFileData.cFileName);
findFilesRecursivelly(nextDirBuffer,fileName,caseSensitive,outputBuffer);
}
while(FindNextFile(hFoundFile,&foundFileData));
按照编码方式,您只是在同一个目录中循环。看起来像缓冲区溢出。在目录树中递归时,您忘记了每个目录都包含对自身的引用(名称为“.”),以及对其父目录的引用(名称为“..”)您必须从递归中排除这些,所以要这样做
do
{
if (wcscmp(foundFileData.cFileName, TEXT(".") == 0 ||
wcscmp(foundFileData.cFileName, TEXT("..") == 0)
{
continue;
}
wcscat(nextDirBuffer,curDir);
wcscat(nextDirBuffer,TEXT("\\"));
wcscat(nextDirBuffer,foundFileData.cFileName);
findFilesRecursivelly(nextDirBuffer,fileName,caseSensitive,outputBuffer);
}
while(FindNextFile(hFoundFile,&foundFileData));
按照编码方式,您只是在同一个目录中循环。每个非根目录都包含自身(“.”)和其父目录(“…”)。您需要明确地从递归搜索中排除这些目录:
if (wcscmp(foundFileData.cFileName, L".") != 0
&& wcscmp(foundFileData.cFileName, L"..") != 0)
{
wcscat(nextDirBuffer,curDir);
wcscat(nextDirBuffer,TEXT("\\"));
wcscat(nextDirBuffer,foundFileData.cFileName);
findFilesRecursivelly(nextDirBuffer,fileName,caseSensitive,outputBuffer);
}
每个非根目录都包含其自身(“.”)及其父目录(“..”)。您需要从递归搜索中明确排除这些目录:
if (wcscmp(foundFileData.cFileName, L".") != 0
&& wcscmp(foundFileData.cFileName, L"..") != 0)
{
wcscat(nextDirBuffer,curDir);
wcscat(nextDirBuffer,TEXT("\\"));
wcscat(nextDirBuffer,foundFileData.cFileName);
findFilesRecursivelly(nextDirBuffer,fileName,caseSensitive,outputBuffer);
}
您是否尝试过调试器?您是否捕获了异常以获取更多详细信息?您是否尝试过在调试器下运行它?是的!这似乎是一个逻辑错误,无法由debbuger自动检测到。因此,当调试器告诉您异常时,调用堆栈上有哪行代码?您没有在输出中分配太多空间缓冲区。可能是溢出?您是否尝试过调试器?是否捕获异常以获取更多详细信息?您是否尝试过在调试器下运行它?是的!这似乎是一个逻辑错误,无法被debbuger自动检测到。因此,当调试器告诉您异常时,调用堆栈上有哪行代码?您没有分配输出缓冲区中的空间过多。是否溢出?