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; // 搜索的關(guān)鍵字
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 ); //還原語(yǔ)言區(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
{
// 過(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:遞歸方法
// 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); // 開始搜索
};
// 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;
// 獲取磁盤驅(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
{
// 過(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)存溢出
}
相關(guān)文章
C++中如何實(shí)現(xiàn)SSL/TLS加密通信
在互聯(lián)網(wǎng)時(shí)代,確保信息傳輸過(guò)程中的機(jī)密性、完整性和可用性成為了開發(fā)者必須考慮的關(guān)鍵因素,在C++網(wǎng)絡(luò)編程中,使用SSL/TLS加密通信是一種常見(jiàn)的做法,它允許客戶端和服務(wù)器之間通過(guò)互聯(lián)網(wǎng)安全地交換信息,從而為網(wǎng)絡(luò)通信提供隱私性和數(shù)據(jù)完整性2025-01-01
C語(yǔ)言深入分析浮點(diǎn)型數(shù)據(jù)存儲(chǔ)
使用編程語(yǔ)言進(jìn)行編程時(shí),需要用到各種變量來(lái)存儲(chǔ)各種信息。變量保留的是它所存儲(chǔ)的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個(gè)變量時(shí),就會(huì)在內(nèi)存中保留一些空間。您可能需要存儲(chǔ)各種數(shù)據(jù)類型的信息,操作系統(tǒng)會(huì)根據(jù)變量的數(shù)據(jù)類型,來(lái)分配內(nèi)存和決定在保留內(nèi)存中存儲(chǔ)什么2022-08-08
C++中函數(shù)使用的基本知識(shí)學(xué)習(xí)教程
這篇文章主要介紹了C++中函數(shù)使用的基本知識(shí)學(xué)習(xí)教程,涵蓋了函數(shù)的聲明和參數(shù)以及指針等各個(gè)方面的知識(shí),非常全面,需要的朋友可以參考下2016-01-01
c語(yǔ)言for、while和do-while循環(huán)之間的區(qū)別
大家好,本篇文章主要講的是c語(yǔ)言for、while和do-while循環(huán)之間的區(qū)別,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
C++實(shí)現(xiàn)關(guān)機(jī)功能詳細(xì)代碼
大家好,本篇文章主要講的是C++實(shí)現(xiàn)關(guān)機(jī)功能詳細(xì)代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
c++動(dòng)態(tài)內(nèi)存管理與智能指針的相關(guān)知識(shí)點(diǎn)
為了更容易同時(shí)也更安全地使用動(dòng)態(tài)內(nèi)存,新的標(biāo)準(zhǔn)庫(kù)提供了兩種智能指針(smart pointer)類型來(lái)管理對(duì)象,下面這篇文章主要給大家介紹了關(guān)于c++動(dòng)態(tài)內(nèi)存管理與智能指針的相關(guān)知識(shí)點(diǎn),需要的朋友可以參考下2022-03-03
C++11中可變模板參數(shù)的實(shí)現(xiàn)
C++11的可變參數(shù)模板允許創(chuàng)建可以接受可變參數(shù)的函數(shù)和類模板,通過(guò)遞歸展開參數(shù)包來(lái)處理每個(gè)參數(shù),下面就來(lái)介紹一下,感興趣的可以了解一下2024-12-12
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

