c++实现广度优先和深度优先查找目录结构

//************************************
// Method:    深度优先法实现扫描目录, 使用递归方式
// FullName:  CScanDirectoryDlg::ScanDirectoryByDeep
// Access:    public static 
// Returns:   int 返回历时
// Parameter: CString& csTextOut [out]
// Parameter: LPCTSTR pszRoot
//************************************

unsigned long ScanDirectoryByDeep( CString& csTextOut, LPCTSTR pszRoot, int nDeep )
{
CString csFileList;
CFileFind finder;
unsigned long nTimeSpan = 0;


if (0 == nDeep)
{
nTimeSpan = GetCurTimeStamp();
}


// build a string with wildcards
CString csDir(pszRoot);

// start working for files
BOOL bWorking = finder.FindFile(csDir + _T("\\*.*"));


//遍历csDir目录下的文件和子目录
while (bWorking)
{
bWorking = finder.FindNextFile();


// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
{
continue;
}


CString csFileOrDir = finder.GetFilePath();
// if it's a directory
if (finder.IsDirectory())
{
ScanDirectoryByDeep(csTextOut, csFileOrDir, nDeep + 1);
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir);
}
else //it's a file
{
csFileList.AppendFormat(_T("[%u-F]%s\r\n"), nDeep, csFileOrDir);
}
}


finder.Close();


//将文件名附进来
csTextOut += csFileList;


//根层进入时统计时间
if (0 == nDeep)
{
nTimeSpan = GetCurTimeStamp() - nTimeSpan;
}
return nTimeSpan;
}


//************************************
// Method:    实现广度扫描
// FullName:  CScanDirectoryDlg::ScanDirectoryByBreadth
// Access:    public static 
// Returns:   unsigned long 返回历时
// Parameter: CString & csTextOut
// Parameter: LPCTSTR pszRoot
// Parameter: int nDeep 层次 (目录层次输出暂未实现)

//************************************
unsigned long ScanDirectoryByBreadth( CString& csTextOut, LPCTSTR pszRoot, int nDeep )
{
// 搜索该路径下的文件
deque<CString> deDirectory;


CFileFind finder;
BOOL bWorking;
unsigned long nTimeSpan;


nTimeSpan = GetCurTimeStamp(); //time count start


CString csDir(pszRoot);
nDeep = 0;
bWorking = finder.FindFile(csDir + _T("\\*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files;
if (finder.IsDots())
{
continue;
}


// if it's a directory
CString strSubDir = finder.GetFilePath();
if (finder.IsDirectory())
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, strSubDir);//记录下文件目录名

deDirectory.push_back(strSubDir);
}
else //it's a file
{
csTextOut.AppendFormat(_T("[%u-F]%s\r\n"), nDeep, strSubDir); // 记录下文件名
}
}


// 寻找一级目录下的子目录或文件
++nDeep;
while(!deDirectory.empty())
{
std::deque<CString>::iterator it = deDirectory.begin();
CFileFind finder;


BOOL bWorking = finder.FindFile((*it) + _T("\\*.*"));
while(bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
{
continue;
}
// if it's a directory
CString csFileOrDir = finder.GetFilePath();
if (finder.IsDirectory())
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir); //记录下文件目录名
deDirectory.push_back(csFileOrDir);
}
else //it's a file
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir);  // 记录下文件名
}
}
// 删除队列的头元素
deDirectory.pop_front();
}


nTimeSpan = GetCurTimeStamp() - nTimeSpan; //time count end
return nTimeSpan;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值