C++非递归遍历磁盘文件和递归遍历磁盘文件的程序示例

 更新时间:2013年11月24日 09:49:18   作者:  
这篇文章主要介绍了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);            // 关闭句柄,不然造成内存溢出
}

相关文章

  • 详解原码、反码与补码存储与大小

    详解原码、反码与补码存储与大小

    这篇文章主要介绍了详解原码、反码与补码存储与大小的相关资料,需要的朋友可以参考下
    2017-06-06
  • VC++植物大战僵尸中文版修改器实现代码

    VC++植物大战僵尸中文版修改器实现代码

    这篇文章主要介绍了VC++植物大战僵尸中文版修改器实现代码,可实现植物大战僵尸中的无限阳光与无冷却时间功能,需要的朋友可以参考下
    2015-04-04
  • 基于VC编写COM连接点事件的分析介绍

    基于VC编写COM连接点事件的分析介绍

    本篇文章是对VC编写COM连接点事件进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言编程PAT乙级学习笔记示例分享

    C语言编程PAT乙级学习笔记示例分享

    这篇文章主要为大家介绍了C语言编程PAT乙级学习笔记实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • C++数据结构之单链表

    C++数据结构之单链表

    这篇文章主要介绍了C++数据结构之单链表,链表是由一个个结点链结成的。结点包括数据域和指针域两部分,数据域用来存储数据元素的信息,指针域用来存储下一个结点的地址,更详细内容请需要的小伙伴参考下面文章内容
    2022-01-01
  • C++将保存char、int 和double到txt文件中

    C++将保存char、int 和double到txt文件中

    这篇文章主要介绍了C++如何将保存char、int 和double到txt文件中,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • EasyC++模板显式具体化

    EasyC++模板显式具体化

    这篇文章主要介绍了C++模板显式具体化,在C++中,可以提供一个具体化函数定义称为具体显式化(explict specialization)。其中包含所需的代码,当编译器找到与函数调用匹配的具体化定义时,将使用该定义,而不再寻找模板,下面我们来看看文章具体介绍吧
    2021-12-12
  • C++11中条件标量和互斥锁应用出现死锁问题

    C++11中条件标量和互斥锁应用出现死锁问题

    这篇文章主要介绍了C++11中条件标量和互斥锁应用出现死锁思考,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • C++引用和结构体介绍

    C++引用和结构体介绍

    这篇文章主要介绍了C++引用和结构体,结构体是我们自定义的复合类型,本质上也是一种变量类型,所以一样可以使用引用,下面来看看文章内容详细介绍,需要的朋友可以参考一下
    2021-11-11
  • c语言中&的用法示例代码

    c语言中&的用法示例代码

    这篇文章主要给大家介绍了关于c语言中&的用法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09

最新评论