• 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);            // 关闭句柄,不然造成内存溢出
    }
  • 相关阅读:
    至少有K个重复字符的最长字串 分治法+递归
    旋转数组
    旋转链表 指针移动
    通过删除字母匹配到字典里最长的单词
    How to fix yum after CentOS 6 went EOL
    Centos6 yum源配置(CentOS 6停止更新)
    ORACLE 12C R2 RAC 安装配置指南
    Background Processes Specific to Oracle RAC
    如何缩小 Oracle 数据库中的临时表空间
    Script To Get Tablespace Utilization In Oracle Database 12c
  • 原文地址:https://www.cnblogs.com/calm2012/p/3229380.html
Copyright © 2020-2023  润新知