欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++非遞歸遍歷磁盤文件和遞歸遍歷磁盤文件的程序示例

 更新時(shí)間:2013年11月24日 09:49:18   作者:  
這篇文章主要介紹了C++非遞歸遍歷磁盤文件和遞歸遍歷磁盤文件的程序示例,大家可以參考使用二種方法

1:非遞歸方法:

復(fù)制代碼 代碼如下:

// 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;    // 搜索的關(guān)鍵字
    std::stack<CString> strPathStack;            // 棧,保存磁盤ID

    void ListAllFileInDrectory(CString strPath);


public:
    Search();
    ~Search();

    void Start(void);                    // 開始搜索
};

復(fù)制代碼 代碼如下:

// 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 ); //還原語言區(qū)域的設(shè)置
        free( old_locale );//還原區(qū)域設(shè)定

        file.Close();
    }

    TCHAR strBuffer[50] = {0};
    TCHAR * pStr = strBuffer;
    CString strTempName;

    // 獲取磁盤驅(qū)動(dòng)器
    GetLogicalDriveStrings(50, strBuffer);

    strTempName = strBuffer;
    while (strTempName != _T(""))
    {
        // 如果是磁盤號(hào)
        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)
    {
        //"錯(cuò)誤碼:" 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;        // 跳出循環(huán)
                    }
                }
            }

            // 如果是目錄 且不是系統(tǒng)屬性目錄 壓棧
            if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
            {
                    strPathStack.push(strPath + _T("\\") + FindFileData.cFileName);
            }
        }
        while(FindNextFile(hListFile, &FindFileData));
    }

    FindClose(hListFile);            // 關(guān)閉句柄,不然造成內(nèi)存溢出
}

2:遞歸方法

復(fù)制代碼 代碼如下:

// 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;    // 搜索的關(guān)鍵字

    void ListAllFileInDrectory(CString strPath);


public:
    Search();
    ~Search();

    void Start(void);                    // 開始搜索
};

復(fù)制代碼 代碼如下:

// 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 ); //還原語言區(qū)域的設(shè)置
        free( old_locale );//還原區(qū)域設(shè)定

        file.Close();
    }

    TCHAR strBuffer[50] = {0};
    TCHAR * pStr = strBuffer;
    CString strTempName;

    // 獲取磁盤驅(qū)動(dòng)器
    GetLogicalDriveStrings(50, strBuffer);

    strTempName = strBuffer;
    while (strTempName != _T(""))
    {
        // 如果是磁盤號(hào)
        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)
    {
        //"錯(cuò)誤碼:" 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;        // 跳出循環(huán)
                    }
                }
            }

            // 如果是目錄 且不是系統(tǒng)屬性目錄 遞歸調(diào)用
            if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
            {
                    ListAllFileInDrectory(strPath + _T("\\") + FindFileData.cFileName);
            }
        }
        while(FindNextFile(hListFile, &FindFileData));
    }

    FindClose(hListFile);            // 關(guān)閉句柄,不然造成內(nèi)存溢出
}

相關(guān)文章

  • 詳解原碼、反碼與補(bǔ)碼存儲(chǔ)與大小

    詳解原碼、反碼與補(bǔ)碼存儲(chǔ)與大小

    這篇文章主要介紹了詳解原碼、反碼與補(bǔ)碼存儲(chǔ)與大小的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • VC++植物大戰(zhàn)僵尸中文版修改器實(shí)現(xiàn)代碼

    VC++植物大戰(zhàn)僵尸中文版修改器實(shí)現(xiàn)代碼

    這篇文章主要介紹了VC++植物大戰(zhàn)僵尸中文版修改器實(shí)現(xiàn)代碼,可實(shí)現(xiàn)植物大戰(zhàn)僵尸中的無限陽光與無冷卻時(shí)間功能,需要的朋友可以參考下
    2015-04-04
  • 基于VC編寫COM連接點(diǎn)事件的分析介紹

    基于VC編寫COM連接點(diǎn)事件的分析介紹

    本篇文章是對(duì)VC編寫COM連接點(diǎn)事件進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言編程PAT乙級(jí)學(xué)習(xí)筆記示例分享

    C語言編程PAT乙級(jí)學(xué)習(xí)筆記示例分享

    這篇文章主要為大家介紹了C語言編程PAT乙級(jí)學(xué)習(xí)筆記實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • C++數(shù)據(jù)結(jié)構(gòu)之單鏈表

    C++數(shù)據(jù)結(jié)構(gòu)之單鏈表

    這篇文章主要介紹了C++數(shù)據(jù)結(jié)構(gòu)之單鏈表,鏈表是由一個(gè)個(gè)結(jié)點(diǎn)鏈結(jié)成的。結(jié)點(diǎn)包括數(shù)據(jù)域和指針域兩部分,數(shù)據(jù)域用來存儲(chǔ)數(shù)據(jù)元素的信息,指針域用來存儲(chǔ)下一個(gè)結(jié)點(diǎn)的地址,更詳細(xì)內(nèi)容請(qǐng)需要的小伙伴參考下面文章內(nèi)容
    2022-01-01
  • C++將保存char、int 和double到txt文件中

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

    這篇文章主要介紹了C++如何將保存char、int 和double到txt文件中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • EasyC++模板顯式具體化

    EasyC++模板顯式具體化

    這篇文章主要介紹了C++模板顯式具體化,在C++中,可以提供一個(gè)具體化函數(shù)定義稱為具體顯式化(explict?specialization)。其中包含所需的代碼,當(dāng)編譯器找到與函數(shù)調(diào)用匹配的具體化定義時(shí),將使用該定義,而不再尋找模板,下面我們來看看文章具體介紹吧
    2021-12-12
  • C++11中條件標(biāo)量和互斥鎖應(yīng)用出現(xiàn)死鎖問題

    C++11中條件標(biāo)量和互斥鎖應(yīng)用出現(xiàn)死鎖問題

    這篇文章主要介紹了C++11中條件標(biāo)量和互斥鎖應(yīng)用出現(xiàn)死鎖思考,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • C++引用和結(jié)構(gòu)體介紹

    C++引用和結(jié)構(gòu)體介紹

    這篇文章主要介紹了C++引用和結(jié)構(gòu)體,結(jié)構(gòu)體是我們自定義的復(fù)合類型,本質(zhì)上也是一種變量類型,所以一樣可以使用引用,下面來看看文章內(nèi)容詳細(xì)介紹,需要的朋友可以參考一下
    2021-11-11
  • c語言中&的用法示例代碼

    c語言中&的用法示例代碼

    這篇文章主要給大家介紹了關(guān)于c語言中&的用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評(píng)論