楼主前些天用_findfirst findnext 遍历c盘,遇到权限问题,这几天个改用findfirstfile api,倒是成功遍历,也不需要访问权限,应该是微软api支持比较好的原因。遍历有点慢,想开几个线程,但是在_beginThreadex 启动线程,线程里参数会有问题<读取字符串的字符时出错>,导致无论你遍历哪一个盘,启动的线程都会到工作空间所在的磁盘遍历,反而速度还大大降低了。
代码:
#include "stdafx.h"
#include "windows.h"
#include "iostream"
#include "string"
#include <process.h>
void FindFile(const std::string strPath);
unsigned int _stdcall ThreadFind(LPVOID lParam)
{
char *szPath = (char *)lParam;
std::cout << std::endl << "建立一个线程进入 " << szPath << std::endl;
FindFile(szPath);
std::cout << std::endl << "退出线程 " << szPath << std::endl;
return 0;
}
void FindFile(const std::string strPath)
{
WIN32_FIND_DATAA findData = { 0 };
std::string strFindPath = strPath + "\\*.*";
HANDLE handle = FindFirstFileA(strFindPath.c_str(), &findData);
if (INVALID_HANDLE_VALUE == handle)
printf("Error:%d\n", GetLastError());
do
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] == '.')
{
continue;
}
//再次调用FindFile
//文件夹 名字为.的代表的就是本目录
//文件夹 名字为..的代表的就是上一层目录
std::cout << "找到一个文件夹" << findData.cFileName << std::endl;
std::string strNeedFindPath = strPath + "\\" + findData.cFileName;
FindFile(strNeedFindPath);
}
else
{
std::cout << "在\t" << strPath << "找到文件:\t" << findData.cFileName << std::endl;
}
} while (FindNextFileA(handle, &findData));
FindClose(handle);
}
int main()
{
using std::string;
string strFilePath = "e:\\";
string strFindPath = strFilePath + "*.*";
WIN32_FIND_DATAA findData = { 0 };
HANDLE handle = FindFirstFileA(strFindPath.c_str(), &findData);
if (INVALID_HANDLE_VALUE == handle)
printf("Error:%d\n", GetLastError());
do
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
std::cout << "找到一个文件夹" << findData.cFileName << std::endl;
std::string strNeedFindPath = strFilePath + findData.cFileName;
//FindFile(strNeedFindPath);
CloseHandle((HANDLE)_beginthreadex(nullptr, 0, ThreadFind, (LPVOID)strNeedFindPath.c_str(), 0, nullptr));
}
else
{
std::cout << "找到一个文件" << findData.cFileName << std::endl;
}
} while (FindNextFileA(handle, &findData));
system("pause");
return 0;
}
代码:
#include "stdafx.h"
#include "windows.h"
#include "iostream"
#include "string"
#include <process.h>
void FindFile(const std::string strPath);
unsigned int _stdcall ThreadFind(LPVOID lParam)
{
char *szPath = (char *)lParam;
std::cout << std::endl << "建立一个线程进入 " << szPath << std::endl;
FindFile(szPath);
std::cout << std::endl << "退出线程 " << szPath << std::endl;
return 0;
}
void FindFile(const std::string strPath)
{
WIN32_FIND_DATAA findData = { 0 };
std::string strFindPath = strPath + "\\*.*";
HANDLE handle = FindFirstFileA(strFindPath.c_str(), &findData);
if (INVALID_HANDLE_VALUE == handle)
printf("Error:%d\n", GetLastError());
do
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] == '.')
{
continue;
}
//再次调用FindFile
//文件夹 名字为.的代表的就是本目录
//文件夹 名字为..的代表的就是上一层目录
std::cout << "找到一个文件夹" << findData.cFileName << std::endl;
std::string strNeedFindPath = strPath + "\\" + findData.cFileName;
FindFile(strNeedFindPath);
}
else
{
std::cout << "在\t" << strPath << "找到文件:\t" << findData.cFileName << std::endl;
}
} while (FindNextFileA(handle, &findData));
FindClose(handle);
}
int main()
{
using std::string;
string strFilePath = "e:\\";
string strFindPath = strFilePath + "*.*";
WIN32_FIND_DATAA findData = { 0 };
HANDLE handle = FindFirstFileA(strFindPath.c_str(), &findData);
if (INVALID_HANDLE_VALUE == handle)
printf("Error:%d\n", GetLastError());
do
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
std::cout << "找到一个文件夹" << findData.cFileName << std::endl;
std::string strNeedFindPath = strFilePath + findData.cFileName;
//FindFile(strNeedFindPath);
CloseHandle((HANDLE)_beginthreadex(nullptr, 0, ThreadFind, (LPVOID)strNeedFindPath.c_str(), 0, nullptr));
}
else
{
std::cout << "找到一个文件" << findData.cFileName << std::endl;
}
} while (FindNextFileA(handle, &findData));
system("pause");
return 0;
}