#include <Windows.h>
#include <tchar.h>
#include <string>
#include <list>
#include <stdio.h>
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
#include <iostream>
using namespace std;
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
#include <process.h>
#include <stdlib.h>
#include <time.h>
CRITICAL_SECTION g_criticalsection;
struct THREADPARAMS
{
FILE * fp;
tstring strDir;
};
void WriteIntoFile(FILE* fp, LPCTSTR lpStr)
{
if (fp != NULL)
{
DWORD len = _tcslen(lpStr);
DWORD rlen;
EnterCriticalSection(&g_criticalsection);
_ftprintf(fp, _T("%s \r\n"), lpStr);
LeaveCriticalSection(&g_criticalsection);
}
}
void DepthFirstSearch(FILE*fp, tstring strpath = _T("c:\\windows"))
{
WriteIntoFile(fp, strpath.c_str());
static int filenum = 0;
TCHAR destDir[MAX_PATH] = {0};
_tcsncpy(destDir, strpath.c_str(), MAX_PATH);
PathAddBackslash(destDir);
PathAppend(destDir, _T("*"));
WIN32_FIND_DATA findData;
memset(&findData, 0, sizeof(findData));
HANDLE hfile = FindFirstFile(destDir, &findData);
if (hfile == INVALID_HANDLE_VALUE)
{
return;
}
do
{
if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && findData.cFileName[0] != _T('.'))
{
wsprintf(destDir, _T("%s"), strpath.c_str());
PathRemoveBackslash(destDir);
wsprintf(destDir, _T("%s\\%s"), destDir, findData.cFileName);
//WriteIntoFile(fp, destDir);
DepthFirstSearch(fp, destDir);
}
else if(findData.cFileName[0] != _T('.'))
{
//cout<<filenum++<<" "<<findData.cFileName<<endl;
TCHAR tmp[MAX_PATH];
wsprintf(tmp, _T("%d -> %s"), filenum++, findData.cFileName);
WriteIntoFile(fp, tmp);
}
} while (FindNextFile(hfile, &findData));
FindClose(hfile);
}
void BreadthFirstSearch(FILE*fp, std::list<tstring>& dirlist, int& fileNum, tstring strPath = _T("c:\\windows"))
{
WriteIntoFile(fp, strPath.c_str());
TCHAR destDir[MAX_PATH] = {0};
_tcsncpy(destDir, strPath.c_str(), MAX_PATH);
PathAddBackslash(destDir);
lstrcat(destDir, _T("*"));
WIN32_FIND_DATA findData;
memset(&findData, 0, sizeof(findData));
HANDLE hFindFile = FindFirstFile(destDir, &findData);
if (hFindFile != INVALID_HANDLE_VALUE)
{
do
{
if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && findData.cFileName[0] != L'.')
{
wsprintf(destDir, _T("%s\\%s"), strPath.c_str(), findData.cFileName);
//WriteIntoFile(fp, destDir);
dirlist.push_back(destDir);
}
else if(findData.cFileName[0] != L'.')
{
//wcout<<fileNum++<<" "<<findData.cFileName<<endl;
//TCHAR tmp[MAX_PATH] = {0};
//_itoa(fileNum++, tmp, 10);
//str.append(findData.cFileName);
TCHAR tmp[MAX_PATH];
wsprintf(tmp, _T("%d -> %s"), fileNum++, findData.cFileName);
WriteIntoFile(fp, tmp);
}
} while (FindNextFile(hFindFile, &findData));
}
FindClose(hFindFile);
}
unsigned __stdcall ThreadProcD(LPVOID lpParam)
{
THREADPARAMS* lpThdParam = (THREADPARAMS*)lpParam;
FILE* fp = lpThdParam->fp;
tstring dir = lpThdParam->strDir;
if(fp == NULL)
{
return -1;
}
if(dir.empty())
dir = _T("C:\\Windows");
DepthFirstSearch(fp, dir);
return 0;
}
unsigned __stdcall ThreadProcB(LPVOID lpParam)
{
THREADPARAMS* lpThdParam = (THREADPARAMS*)lpParam;
FILE* fp = lpThdParam->fp;
tstring dir = lpThdParam->strDir;
if(fp == NULL)
{
return -1;
}
if(dir.empty())
dir = _T("C:\\Windows");
int filenum = 0;
std::list<tstring> dirlist;
BreadthFirstSearch(fp, dirlist, filenum, dir.c_str());
while(!dirlist.empty())
{
tstring currDir = *dirlist.begin();
BreadthFirstSearch(fp, dirlist, filenum, currDir);
dirlist.pop_front();
}
return 0;
}
void Funbreadthfirstsearch(tstring file, tstring dir=_T("C:\\Windows"))
{
THREADPARAMS* lpThdParam;
lpThdParam = (THREADPARAMS*)malloc(sizeof(THREADPARAMS));
ZeroMemory(lpThdParam, sizeof(THREADPARAMS));
FILE* fp = _tfopen(file.c_str(), _T("wb"));
lpThdParam->fp = fp;
lpThdParam->strDir = dir;
HANDLE thd;
unsigned tid;
thd = (HANDLE)_beginthreadex(NULL, 0, ThreadProcB, lpThdParam, 0, &tid);
WaitForSingleObject(thd, INFINITE);
CloseHandle((HANDLE)thd);
fclose(fp);
free(lpThdParam);
}
void FunDepthFirstSearch(tstring file, tstring dir=_T("C:\\Windows"))
{
THREADPARAMS* lpThdParam;
lpThdParam = (THREADPARAMS*)malloc(sizeof(THREADPARAMS));
ZeroMemory(lpThdParam, sizeof(THREADPARAMS));
FILE* fp = _tfopen(file.c_str(), _T("wb"));
lpThdParam->fp = fp;
lpThdParam->strDir = dir;
HANDLE thd;
unsigned tid;
thd = (HANDLE)_beginthreadex(NULL, 0, ThreadProcD, lpThdParam, 0, &tid);
WaitForSingleObject(thd, INFINITE);
CloseHandle((HANDLE)thd);
fclose(fp);
free(lpThdParam);
}
int _tmain(int agrc, _TCHAR* argv[])
{
//cout<<argv[0]<<endl;
//cout<<argv[1]<<endl;
TCHAR* searchDir = _T("c:\\Windows");
if(agrc >= 2)
{
if(PathFileExists(argv[1]))
searchDir = argv[1];
}
try
{
InitializeCriticalSection(&g_criticalsection);
clock_t start, finish;
start = clock();
cout<<"搜索的路径是:"<<searchDir<<endl;
FunDepthFirstSearch(_T("d:\\tmp1.txt"), searchDir);
finish = clock();
cout<<"深度搜索的耗时长为:"<<(double)(finish - start)<<endl;
cout<<"结果保存路径"<<_T("d:\\tmp2.txt\r\n\r\n")<<endl;
start = clock();
cout<<"搜索的路径是:"<<searchDir<<endl;
Funbreadthfirstsearch(_T("d:\\tmp2.txt"), searchDir);
finish = clock();
cout<<"广度搜索的耗时长为:"<<(double)(finish - start)<<endl;
cout<<"结果保存路径"<<_T("d:\\tmp2.txt")<<endl;
DeleteCriticalSection(&g_criticalsection);
}
catch(...)
{
DeleteCriticalSection(&g_criticalsection);
}
system("pause");
return 0;
}
广度 深度 遍历文件夹
最新推荐文章于 2022-03-30 23:45:02 发布