非递归的算法检索目录及子文件

1、VC 共享DLL工程

#include <iostream>
#include <math.h>
#include <conio.h>
#include <afxwin.h>
#include <shlwapi.h>   
using namespace std;

typedef int(*lpAddFun)(int, int); //宏定义函数指针类型
typedef BOOL (*PROCESS_FILE_FUNCTION)(CString filename);
//上面的PROCESS_FILE_FUNCTION是一种函数指针,这个函数处理文件名为filename的文件,如果该函数返回
//FALSE,则ProcessDirectory立刻退出,不再继续查找
void ProcessDirectory(LPCTSTR dirname,PROCESS_FILE_FUNCTION proc,CStringArray &retDirs)
{
	CStringArray dirs;

	CString searchname;
	CFileFind find;
	dirs.Add(dirname);
	BOOL bRet;
	while(dirs.GetSize()>0)
	{

		searchname = dirs[0] +"\\*.*";
		dirs.RemoveAt(0);
		bRet = find.FindFile (searchname,0);
		if(!bRet)continue;
		do{
			bRet = find.FindNextFile ();
			if(find.IsDots ())
			{//忽略.和..文件
				continue;
			}
			if(find.IsDirectory ())
			{
				dirs.Add (find.GetFilePath());
				continue;
			}else{
				if(proc(find.GetFilePath ()))
				{
					retDirs.Add(find.GetFilePath());
				}
			}
		}while(bRet);
	}
}

BOOL exe(CString filename)
{
	CString ext=PathFindExtension(filename);
	//修改内容,过滤文件
	if(ext.CompareNoCase(_T(".h"))==0)
	{
		return TRUE;
	}else{
		return FALSE;
	}
}
int main()
{
	CStringArray dirs;
	ProcessDirectory(_T("d:\\openssl"),(PROCESS_FILE_FUNCTION)exe,dirs);
	for(int i=0;i<dirs.GetSize();i++)
	{
		CString val=dirs.GetAt(i);
		val.Append(_T("\n"));
		OutputDebugString(val);
	}
	getchar();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值