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. 毕设(1)——机械臂DH建模

    目录 毕设(1)--机械臂DH建模 改进DH参数表 Matlab代码验证 毕设中用到了很多代码,其中一部分我通过看书和看论文学习并实现的代码,会通过Gitee仓库分享出来,这些代码仅用于学习使用,祝各 ...

  2. Python if-else的简单表示

    常见写法 a = 1 b = 1 c = 2 if a == b: print("true") elif a == c: print("false") else ...

  3. 写了个基于 MacOS + iTerm2 自动打开窗口执行命令的工具

    大家好,我是秋风,今天要给大家带来的这个工具是我最近写的 一个 npm 工具.mmt 是基于 MacOS + iTerm2 ,目的主要是为了提高日常生活中的效率,接下来我带大家看看一些常用的一些场景. ...

  4. 【Nginx】Windows平台下配置Nginx服务实现负载均衡

    前言:废话不多说了,直接上步骤. 系统环境:win10 测试用的开发环境和服务类型:VS2022 + DotNet 6 + WebApi 1.本地先创建一个webapi项目,用于测试使用. 2.新建一 ...

  5. ansible对文件内容操作

    ansible lineinfile 简介 lineinfile该模块是操作文件中的每一行内容,他是按照行为单位的,和下面的replace模块并不冲突. 修改匹配行,如果不存在就会添加 tasks: ...

  6. 令你瞠目结舌的 Python 代码技巧

    0. for-else 在 Python 中,else 不仅可以与 if 搭配使用,还可以与 for 结合. python答疑 咨询 学习交流群2:660193417### for x in rang ...

  7. NC19115 选择颜色

    NC19115 选择颜色 题目 题目描述 \(n\) 个人排成一个环形,每个人要从 \(c\) 种颜色中选择一个. 牛牛希望相邻的人选择的颜色是不同的 问有多少种方案. 输出方案数对 \(10007\ ...

  8. NC20565 [SCOI2009]生日礼物

    NC20565 [SCOI2009]生日礼物 题目 题目描述 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有 \(N\) 个,分为 \(K\) 种.简单的说,可以将彩带考虑为 \(x\) ...

  9. Keyboading 思路

    0x01 前置芝士 还是先放个 link 吧. 所需知识点:BFS. 思维难度较高,实现简单. 0x02 题目大意:其实就是给你个图,按顺序走到相应的点,求所需最少步数(走到需要去的点会耗费一次步数) ...

  10. 【FAQ】接入HMS Core推送服务,服务端下发消息常见错误码原因分析及解决方法

    HMS Core推送服务支持开发者使用HTTPS协议接入Push服务端,可以从服务器发送下行消息给终端设备.这篇文章汇总了服务端下发消息最常见的6个错误码,并提供了原因分析和解决方法,有遇到类似问题的 ...