Recursive file search using C++ MFC?(使用 C++ MFC 进行递归文件搜索?)
问题描述
使用 C++ 和 MFC 递归搜索文件的最简洁方法是什么?
What is the cleanest way to recursively search for files using C++ and MFC?
这些解决方案中的任何一个都提供通过一次通过使用多个过滤器的能力吗?我想使用 CFileFind 我可以过滤 *.* 然后编写自定义代码以进一步过滤到不同的文件类型.是否提供内置的多个过滤器(即 *.exe、*.dll)?
Do any of these solutions offer the ability to use multiple filters through one pass? I guess with CFileFind I could filter on *.* and then write custom code to further filter into different file types. Does anything offer built-in multiple filters (ie. *.exe,*.dll)?
刚刚意识到我所做的一个明显假设使我之前的 EDIT 无效.如果我尝试使用 CFileFind 进行递归搜索,我必须使用 *.* 作为我的通配符,否则将无法匹配子目录并且不会发生递归.因此,无论如何都必须单独处理对不同文件扩展名的过滤.
Just realized an obvious assumption that I was making that makes my previous EDIT invalid. If I am trying to do a recursive search with CFileFind, I have to use *.* as my wildcard because otherwise subdirectories won't be matched and no recursion will take place. So filtering on different file-extentions will have to be handled separately regardless.
推荐答案
使用 CFileFind.
看看这个示例来自MSDN:
Take a look at this example from MSDN:
void Recurse(LPCTSTR pstr)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
// if it's a directory, recursively search it
if (finder.IsDirectory())
{
CString str = finder.GetFilePath();
cout << (LPCTSTR) str << endl;
Recurse(str);
}
}
finder.Close();
}
这篇关于使用 C++ MFC 进行递归文件搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 C++ MFC 进行递归文件搜索?
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
