广度 深度 遍历文件夹

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值