欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++非遞歸遍歷磁盤(pán)文件和遞歸遍歷磁盤(pán)文件的程序示例

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

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;            // 棧,保存磁盤(pán)ID

    void ListAllFileInDrectory(CString strPath);


public:
    Search();
    ~Search();

    void Start(void);                    // 開(kāi)始搜索
};

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

        file.Close();
    }

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

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

    strTempName = strBuffer;
    while (strTempName != _T(""))
    {
        // 如果是磁盤(pán)號(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
        {
            // 過(guò)濾"."和".."
            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);                    // 開(kāi)始搜索
};

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

        file.Close();
    }

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

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

    strTempName = strBuffer;
    while (strTempName != _T(""))
    {
        // 如果是磁盤(pán)號(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
        {
            // 過(guò)濾"."和".."
            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)存溢出
}

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有