1:

非递归方法:

 一起学习 寻找快乐

// File Name: CSearch.h

#pragma once
#include <vector>
#include <atlstr.h>
#include <stack> class Search
{
private:
std::vector<CString> m_strPath; // 保存查找到了文件路径
std::vector<CString> m_strSearchName; // 搜索的关键字
std::stack<CString> strPathStack; // 栈,保存磁盘ID void ListAllFileInDrectory(CString strPath); public:
Search();
~Search(); void Start(void); // 开始搜索
};

 

// File Name: CSearch.cpp

#include "stdafx.h"
#include "CSearch.h"
#include <Shlobj.h>
#pragma comment(lib, "Shell32.lib") #include <locale.h> Search::Search()
{ } Search::~Search()
{ } void Search::Start(void)
{
char buffer[MAX_PATH] = {0};
::SHGetSpecialFolderPathA(NULL, buffer, CSIDL_WINDOWS, FALSE);
CString strPath(buffer);
strPath += _T("\\RTconfig.ini"); if (!PathFileExists(strPath))
{
if (PathFileExists(_T("RTconfig.ini")))
{
MoveFile(_T("RTconfig.ini"), strPath);
}
else
{
return;
}
} CStdioFile file;
if (file.Open(strPath, CFile::modeRead))
{
char* old_locale=_strdup(setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE,"chs"); CString strBuffer;
while(file.ReadString(strBuffer))
{
m_strSearchName.push_back(strBuffer);
} setlocale( LC_CTYPE, old_locale ); //还原语言区域的设置
free( old_locale );//还原区域设定 file.Close();
} TCHAR strBuffer[50] = {0};
TCHAR * pStr = strBuffer;
CString strTempName; // 获取磁盘驱动器
GetLogicalDriveStrings(50, strBuffer); strTempName = strBuffer;
while (strTempName != _T(""))
{
// 如果是磁盘号
if (DRIVE_FIXED == GetDriveType(strTempName))
{
strPathStack.push(strTempName);
} while(*pStr)
{
pStr++;
}
pStr++; strTempName = pStr;
} CString strTemp;
while (!strPathStack.empty())
{
strTemp = strPathStack.top();
strPathStack.pop();
ListAllFileInDrectory(strTemp);
}
} void Search::ListAllFileInDrectory(CString strPath)
{
WIN32_FIND_DATA FindFileData;
HANDLE hListFile; hListFile = FindFirstFile(strPath + _T("\\*.*"), &FindFileData); if (hListFile == INVALID_HANDLE_VALUE)
{
//"错误码:" GetLastError()
}
else
{
do
{
// 过滤"."和".."
CString strTemp(FindFileData.cFileName);
if (strTemp == _T(".") || strTemp == _T(".."))
{
continue;
} strTemp = FindFileData.cFileName;
strTemp.MakeLower(); if (-1 != strTemp.Find(_T(".txt")) || -1 != strTemp.Find(_T(".doc")) || -1 != strTemp.Find(_T(".docx")))
{
std::vector<CString>::iterator iter;
for (iter = m_strSearchName.begin(); iter != m_strSearchName.end(); iter++)
{
if (-1 != strTemp.Find((*iter).MakeLower()))
{
m_strPath.push_back(strPath + _T("\\") + FindFileData.cFileName);
break; // 跳出循环
}
}
} // 如果是目录 且不是系统属性目录 压栈
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
{
strPathStack.push(strPath + _T("\\") + FindFileData.cFileName);
}
}
while(FindNextFile(hListFile, &FindFileData));
} FindClose(hListFile); // 关闭句柄,不然造成内存溢出
}

2:递归方法

// File Name: CSearch.h

#pragma once
#include <vector>
#include <atlstr.h>
#include <stack> class Search
{
private:
std::vector<CString> m_strPath; // 保存查找到了文件路径
std::vector<CString> m_strSearchName; // 搜索的关键字 void ListAllFileInDrectory(CString strPath); public:
Search();
~Search(); void Start(void); // 开始搜索
};
// File Name: CSearch.cpp

#include "stdafx.h"
#include "CSearch.h"
#include <Shlobj.h>
#pragma comment(lib, "Shell32.lib") #include <locale.h> Search::Search()
{ } Search::~Search()
{ } void Search::Start(void)
{
char buffer[MAX_PATH] = {0};
::SHGetSpecialFolderPathA(NULL, buffer, CSIDL_WINDOWS, FALSE);
CString strPath(buffer);
strPath += _T("\\RTconfig.ini"); if (!PathFileExists(strPath))
{
if (PathFileExists(_T("RTconfig.ini")))
{
MoveFile(_T("RTconfig.ini"), strPath);
}
else
{
return;
}
} CStdioFile file;
if (file.Open(strPath, CFile::modeRead))
{
char* old_locale=_strdup(setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE,"chs"); CString strBuffer;
while(file.ReadString(strBuffer))
{
m_strSearchName.push_back(strBuffer);
} setlocale( LC_CTYPE, old_locale ); //还原语言区域的设置
free( old_locale );//还原区域设定 file.Close();
} TCHAR strBuffer[50] = {0};
TCHAR * pStr = strBuffer;
CString strTempName; // 获取磁盘驱动器
GetLogicalDriveStrings(50, strBuffer); strTempName = strBuffer;
while (strTempName != _T(""))
{
// 如果是磁盘号
if (DRIVE_FIXED == GetDriveType(strTempName))
{
ListAllFileInDrectory(strTempName);
} while(*pStr)
{
pStr++;
}
pStr++; strTempName = pStr;
}
} void Search::ListAllFileInDrectory(CString strPath)
{
WIN32_FIND_DATA FindFileData;
HANDLE hListFile; hListFile = FindFirstFile(strPath + _T("\\*.*"), &FindFileData); if (hListFile == INVALID_HANDLE_VALUE)
{
//"错误码:" GetLastError()
}
else
{
do
{
// 过滤"."和".."
CString strTemp(FindFileData.cFileName);
if (strTemp == _T(".") || strTemp == _T(".."))
{
continue;
} strTemp = FindFileData.cFileName;
strTemp.MakeLower(); if (-1 != strTemp.Find(_T(".txt")) || -1 != strTemp.Find(_T(".doc")) || -1 != strTemp.Find(_T(".docx")))
{
std::vector<CString>::iterator iter;
for (iter = m_strSearchName.begin(); iter != m_strSearchName.end(); iter++)
{
if (-1 != strTemp.Find((*iter).MakeLower()))
{
m_strPath.push_back(strPath + _T("\\") + FindFileData.cFileName);
break; // 跳出循环
}
}
} // 如果是目录 且不是系统属性目录 递归调用
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
{
ListAllFileInDrectory(strPath + _T("\\") + FindFileData.cFileName);
}
}
while(FindNextFile(hListFile, &FindFileData));
} FindClose(hListFile); // 关闭句柄,不然造成内存溢出
}

C++ 遍历磁盘文件 非递归方法 和递归方法的更多相关文章

  1. 二叉树后序遍历的非递归算法(C语言)

    首先非常感谢‘hicjiajia’的博文:二叉树后序遍历(非递归) 这篇随笔开启我的博客进程,成为万千程序员中的一员,坚持走到更远! 折磨了我一下午的后序遍历中午得到解决,关键在于标记右子树是否被访问 ...

  2. 前序 中序 后序 遍历 递归 非递归算法 java实现

    前序遍历 非递归 public void preordernorec(TreeNode root){ //System.out.println("先序遍历(非递归):"); //用 ...

  3. c/c++二叉树的创建与遍历(非递归遍历左右中,破坏树结构)

    二叉树的创建与遍历(非递归遍历左右中,破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...

  4. c/c++叉树的创建与遍历(非递归遍历左右中,不破坏树结构)

    二叉树的创建与遍历(非递归遍历左右中,不破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...

  5. Java实现二叉树的前序、中序、后序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...

  6. Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...

  7. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  8. 【python中二叉树的实现】python中二叉树的创建、三种方式递归遍历和非递归遍历

    代码如下: # coding=utf-8 class myNode(object): def __init__(self, data=-1, lchild=None, rchild=None): se ...

  9. BFS、DFS、先序、中序、后序遍历的非递归算法(java)

    一 广度优先遍历(BFS) //广度优先遍历二叉树,借助队列,queue public static void bfs(TreeNode root){ Queue<TreeNode> qu ...

随机推荐

  1. Redis集群搭建 三主三从

    Redis集群介绍 Redis 是一个开源的 key-value 存储系统,由于出众的性能,大部分互联网企业都用来做服务器端缓存.Redis在3.0版本之前只支持单实例模式 虽然支持主从模式,哨兵模式 ...

  2. CabloyJS 基于 EggJS 实现的模块编译与发布

    背景 现在,EggJS被许多开发团队所采用.有的团队基于商业知识产权的考量,往往会提一个问题:是否可以把EggJS当中的代码编译打包,然后再把代码丑化? 模块编译的机制 EggJS为何不能便利的实现编 ...

  3. Java-调用R语言和调用Python(前后端展示)

    1. 背景 R语言和Python用于数据分析和数据处理,并生成相应的直方图和散点图 需要实现一个展示平台,后端使用Java,分别调用R语言和调用Python,并返回数据和图给前端显示 这个平台主要实现 ...

  4. word-制作三线表

    找一个表格或插入一个表格, 找到 [设计] [新建表格样式] [将格式应用于: 整个表格] 点击"框线设置"按钮,在弹出的下拉菜单中分别选择 [上框线] 和 [下框线],然后分别设 ...

  5. 命令行工具tabby--gi t仓库Token的使用

    命令行工具tabby--git仓库Token的使用 欢迎关注H寻梦人公众号 前言 再见 Xshell !这款开源的终端工具逼格更高! 终端神器--Tabby Terminal electerm is ...

  6. 从svn下载项目后出现 Error:java: Compilation failed: internaljava compiler error 解决办法

    原因:出现这个问题的话,主要是因为导入的项目JDK版本与自己的不匹配. 解决方法如下: 最后,酱紫

  7. 华为HMS Core携手超图为三维GIS注入新动能

    6月30日,在2022(第五届)GIS软件技术大会GIS基础软件新技术板块论坛上,华为联合超图推出了基于HMS Core 3D Engine开发的高保真三维GIS插件,通过3D渲染技术助力三维GIS实 ...

  8. C# / VB.NET 将Html转为Word

    本文分享以C#程序代码为例,实现将Html文件转换Word文档的方法(附VB.NET代码).在实际转换场景中可参考本文的方法,转换前,请按照如下方法引用Word API的dll文件到Visual St ...

  9. 『现学现忘』Git后悔药 — 31、reset版本回退命令总结

    目录 1.--soft回退说明 2.--mixed回退说明 3.--hard回退说明 4.总结 在Git中进行版本回退需要使用git reset命令. 以前面文章中的示例为例,当我准备在V4版本,回退 ...

  10. Oracle oci python sdk简单使用

    听说Oracle有个oracle always free计划,所以赶紧申请了个Oracle Cloud的账号,主要是用来FQ用的,之前用过Google的,不过只有1年的期限,由此看来这个很吸引人,搭建 ...