您的位置:首页 > 其它

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

2013-09-27 10:21 106 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: