C++ 遍历磁盘文件 非递归方法 和递归方法
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++ 遍历磁盘文件 非递归方法 和递归方法的更多相关文章
- 二叉树后序遍历的非递归算法(C语言)
首先非常感谢‘hicjiajia’的博文:二叉树后序遍历(非递归) 这篇随笔开启我的博客进程,成为万千程序员中的一员,坚持走到更远! 折磨了我一下午的后序遍历中午得到解决,关键在于标记右子树是否被访问 ...
- 前序 中序 后序 遍历 递归 非递归算法 java实现
前序遍历 非递归 public void preordernorec(TreeNode root){ //System.out.println("先序遍历(非递归):"); //用 ...
- c/c++二叉树的创建与遍历(非递归遍历左右中,破坏树结构)
二叉树的创建与遍历(非递归遍历左右中,破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...
- c/c++叉树的创建与遍历(非递归遍历左右中,不破坏树结构)
二叉树的创建与遍历(非递归遍历左右中,不破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...
- Java实现二叉树的前序、中序、后序遍历(非递归方法)
在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...
- Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)
在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 【python中二叉树的实现】python中二叉树的创建、三种方式递归遍历和非递归遍历
代码如下: # coding=utf-8 class myNode(object): def __init__(self, data=-1, lchild=None, rchild=None): se ...
- BFS、DFS、先序、中序、后序遍历的非递归算法(java)
一 广度优先遍历(BFS) //广度优先遍历二叉树,借助队列,queue public static void bfs(TreeNode root){ Queue<TreeNode> qu ...
随机推荐
- Linux服务器启动jstatd服务
Linux服务器启动jstatd服务 1.查找jdk所在目录 2.在jdk的bin目录下创建文件jstatd.all.policy touch jstatd.all.policy 3.写入安全配置 g ...
- Node.js精进(2)——异步编程
虽然 Node.js 是单线程的,但是在融合了libuv后,使其有能力非常简单地就构建出高性能和可扩展的网络应用程序. 下图是 Node.js 的简单架构图,基于 V8 和 libuv,其中 Node ...
- 1.设计模式第一步-《设计模式从头到脚舔一遍-使用C#实现》
更新记录: 完成第一次编辑:2022年4月23日20:29:33. 加入小黄人歌曲:2022年4月23日21:45:36. 1.1 设计模式(Design Pattern)是什么 设计模式是理论.是前 ...
- 【Golang】程序如何优雅的退出?
1. 背景 项目开发过程中,随着需求的迭代,代码的发布会频繁进行,在发布过程中,如何让程序做到优雅的退出? 为什么需要优雅的退出? 你的 http 服务,监听端口没有关闭,客户的请求发过来了,但处理了 ...
- Java开发学习(五)----bean的生命周期
一.什么是生命周期 首先理解下什么是生命周期? 从创建到消亡的完整过程,例如人从出生到死亡的整个过程就是一个生命周期. bean生命周期是什么? bean对象从创建到销毁的整体过程. bean生命周期 ...
- 4.怎么理解相互独立事件?真的是没有任何关系的事件吗? 《zobol的考研概率论教程》
1.从条件概率的定义来看独立事件的定义 2.从古典概率的定义来看独立事件的定义 3.P(A|B)和P(A)的关系是什么? 4.由P(AB)=P(A)P(B)推出"独立" 5.从韦恩 ...
- 前端学习 linux —— 第一篇
前端学习 linux - 第一篇 本文主要介绍"linux 发行版本"."cpu 架构"."Linux 目录结构"."vi 和 v ...
- Vue3.0系列——「vue3.0学习手册」第一期
一.项目搭建 vite是尤大大开发的一款意图取代webpack的工具.其实现原理是利用ES6的import发送请求加载文件的特性.拦截这些请求,做一些编译,省去webpack冗长的打包时间.并将其与R ...
- 从开发一款基于Vue技术栈的全栈热重载生产环境脚手架,我学到了什么
浏览文章前 这一期,我分享给大家三点看源码的小技巧,这也是从别的大佬那总结的. 被反复使用的代码 这样的代码是一个软件的重点函数,一个大神的写法有很多精华值得学习. 穿越时间的代码 如果一段代码10年 ...
- NC25136 [USACO 2006 Ope B]Cows on a Leash
NC25136 [USACO 2006 Ope B]Cows on a Leash 题目 题目描述 给定如图所示的若干个长条.你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其 ...