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. frp 用于内网穿透的基本配置和使用

    frp 用于内网穿透的基本配置和使用 今天是端午节,先祝端午安康! frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP.UDP.HTTP.HTTPS 等多种协议.可以将内网服务以安全.便 ...

  2. Groovy基础语法

    Groovy 基础语法 变量定义 1.支持动态类型,使用def关键字定义变量 // Java中定义变量的方式 int age = 18; String name = "张三"; / ...

  3. 微信access_token缓存与更新

    由于Access Token有效期只有7200秒,而每天调用获取的次数只有2000次,所以需要将Access Token进行缓存来保证不触发超过最大调用次数.另外在微信公众平台中,绝大多数高级接口都需 ...

  4. 6.文本三剑客之sed

    文本三剑客之sed 目录 文本三剑客之sed sed编辑器 sed概述 sed工作流程 sed用法 sed打印 sed删除 sed替换 sed增加行内容 sed剪切粘贴与复制粘贴 sed字符/字符串交 ...

  5. 快速保存Win10锁屏壁纸,收获美丽瞬间

    对于写程序而言,每天接触得最多的就是电脑了 所以保持一种开放乐观,豁达美丽的心情是十分有必要的 使用"Everything"工具,输入"LocalState\Assets ...

  6. Sentiment analysis in nlp

    Sentiment analysis in nlp The goal of the program is to analysis the article title is Sarcasm or not ...

  7. rhel挂载本地光盘为yum源

    挂载光盘 mount /dev/sr0 /mnt/cdrom mkdir /mnt/cdrom 临时挂载 mount /dev/sr0 /mnt/cdrom 永久挂载光盘 mount -a 执行挂载 ...

  8. 初学python常用,python模块安装和卸载的几种方法

    兄弟们常常因为遇到模块不会安装,或者遇到报错就懵了,就很耽误学习进度,今天我们就一次性了解Python几种安装模块的方法~不过~ 实在是懒得看 点击此处找管理员小姐姐手把手教你安装 一.命令提示符窗口 ...

  9. 9.4 苹果macOS电脑如何安装Android开发环境(Android Studio)

    下载 来到官方下载界面(需要 科 学 上 网),下载最新版本,点击Download,然后同意协议,在点击下载:如果平常看文档,可以点击Google中国Android开发者官网(部分用户可能也需要科 学 ...

  10. gslb(global server load balance)技术的一点理解

    gslb(global server load balance)技术的一点理解 前言 对于比较大的互联网公司来说,用户可能遍及海内外,此时,为了提升用户体验,公司一般会在离用户较近的地方建立机房,来服 ...