最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間: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ū)動器
    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;        // 跳出循環(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ū)動器
    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;        // 跳出循環(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)文章

  • C語言程序環(huán)境編譯+鏈接理論

    C語言程序環(huán)境編譯+鏈接理論

    這篇文章主要介紹了C語言程序環(huán)境編譯+鏈接理論,下面文章基于C語言的相關(guān)資料展開對編譯和鏈接的詳細(xì)介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • C++中如何實(shí)現(xiàn)SSL/TLS加密通信

    C++中如何實(shí)現(xiàn)SSL/TLS加密通信

    在互聯(lián)網(wǎng)時代,確保信息傳輸過程中的機(jī)密性、完整性和可用性成為了開發(fā)者必須考慮的關(guān)鍵因素,在C++網(wǎng)絡(luò)編程中,使用SSL/TLS加密通信是一種常見的做法,它允許客戶端和服務(wù)器之間通過互聯(lián)網(wǎng)安全地交換信息,從而為網(wǎng)絡(luò)通信提供隱私性和數(shù)據(jù)完整性
    2025-01-01
  • C語言深入分析浮點(diǎn)型數(shù)據(jù)存儲

    C語言深入分析浮點(diǎn)型數(shù)據(jù)存儲

    使用編程語言進(jìn)行編程時,需要用到各種變量來存儲各種信息。變量保留的是它所存儲的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個變量時,就會在內(nèi)存中保留一些空間。您可能需要存儲各種數(shù)據(jù)類型的信息,操作系統(tǒng)會根據(jù)變量的數(shù)據(jù)類型,來分配內(nèi)存和決定在保留內(nèi)存中存儲什么
    2022-08-08
  • C++中函數(shù)使用的基本知識學(xué)習(xí)教程

    C++中函數(shù)使用的基本知識學(xué)習(xí)教程

    這篇文章主要介紹了C++中函數(shù)使用的基本知識學(xué)習(xí)教程,涵蓋了函數(shù)的聲明和參數(shù)以及指針等各個方面的知識,非常全面,需要的朋友可以參考下
    2016-01-01
  • 一篇文章帶你了解c++運(yùn)算符重載

    一篇文章帶你了解c++運(yùn)算符重載

    下面小編就為大家?guī)硪黄钊肜斫釩++運(yùn)算符重載。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-08-08
  • c語言for、while和do-while循環(huán)之間的區(qū)別

    c語言for、while和do-while循環(huán)之間的區(qū)別

    大家好,本篇文章主要講的是c語言for、while和do-while循環(huán)之間的區(qū)別,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • C++實(shí)現(xiàn)關(guān)機(jī)功能詳細(xì)代碼

    C++實(shí)現(xiàn)關(guān)機(jī)功能詳細(xì)代碼

    大家好,本篇文章主要講的是C++實(shí)現(xiàn)關(guān)機(jī)功能詳細(xì)代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • c++動態(tài)內(nèi)存管理與智能指針的相關(guān)知識點(diǎn)

    c++動態(tài)內(nèi)存管理與智能指針的相關(guān)知識點(diǎn)

    為了更容易同時也更安全地使用動態(tài)內(nèi)存,新的標(biāo)準(zhǔn)庫提供了兩種智能指針(smart pointer)類型來管理對象,下面這篇文章主要給大家介紹了關(guān)于c++動態(tài)內(nèi)存管理與智能指針的相關(guān)知識點(diǎn),需要的朋友可以參考下
    2022-03-03
  • C++11中可變模板參數(shù)的實(shí)現(xiàn)

    C++11中可變模板參數(shù)的實(shí)現(xiàn)

    C++11的可變參數(shù)模板允許創(chuàng)建可以接受可變參數(shù)的函數(shù)和類模板,通過遞歸展開參數(shù)包來處理每個參數(shù),下面就來介紹一下,感興趣的可以了解一下
    2024-12-12
  • C語言數(shù)據(jù)結(jié)構(gòu)之簡易計算器

    C語言數(shù)據(jù)結(jié)構(gòu)之簡易計算器

    這篇文章主要為大家詳細(xì)介紹了C語言數(shù)據(jù)結(jié)構(gòu)之簡易計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11

最新評論

鹰潭市| 卢湾区| 华坪县| 丽水市| 东乌珠穆沁旗| 亳州市| 哈密市| 临安市| 海原县| 民县| 桂阳县| 依兰县| 农安县| 馆陶县| 凤城市| 东阳市| 济阳县| 惠来县| 杂多县| 镇雄县| 紫阳县| 三明市| 青川县| 响水县| 桃江县| 枝江市| 光泽县| 湛江市| 福泉市| 久治县| 遂宁市| 开阳县| 南召县| 满洲里市| 拜城县| 于都县| 阳山县| 巴林左旗| 迁西县| 安溪县| 沾益县|