C++ 遍歷目錄下文件簡(jiǎn)單實(shí)現(xiàn)實(shí)例
C++ 遍歷目錄下文件
function:遍歷目錄下所有文件,返回文件總數(shù),子文件夾總數(shù)(修改一下可以獲得全部文件名等)。
實(shí)例代碼:
#include "stdlib.h" #include "direct.h" #include "string.h" #include "io.h" #include "stdio.h" #include "iostream" using namespace std; class CBrowseDir { protected: //存放初始目錄的絕對(duì)路徑,以'\'結(jié)尾 char m_szInitDir[_MAX_PATH]; public: //缺省構(gòu)造器 CBrowseDir(); //設(shè)置初始目錄為dir,如果返回false,表示目錄不可用 bool SetInitDir(const char *dir); //開(kāi)始遍歷初始目錄及其子目錄下由filespec指定類(lèi)型的文件 //filespec可以使用通配符 * ?,不能包含路徑。 //如果返回false,表示遍歷過(guò)程被用戶中止 bool BeginBrowse(const char *filespec); protected: //遍歷目錄dir下由filespec指定的文件 //對(duì)于子目錄,采用迭代的方法 //如果返回false,表示中止遍歷文件 bool BrowseDir(const char *dir,const char *filespec); //函數(shù)BrowseDir每找到一個(gè)文件,就調(diào)用ProcessFile //并把文件名作為參數(shù)傳遞過(guò)去 //如果返回false,表示中止遍歷文件 //用戶可以覆寫(xiě)該函數(shù),加入自己的處理代碼 virtual bool ProcessFile(const char *filename); //函數(shù)BrowseDir每進(jìn)入一個(gè)目錄,就調(diào)用ProcessDir //并把正在處理的目錄名及上一級(jí)目錄名作為參數(shù)傳遞過(guò)去 //如果正在處理的是初始目錄,則parentdir=NULL //用戶可以覆寫(xiě)該函數(shù),加入自己的處理代碼 //比如用戶可以在這里統(tǒng)計(jì)子目錄的個(gè)數(shù) virtual void ProcessDir(const char *currentdir,const char *parentdir); }; CBrowseDir::CBrowseDir() { //用當(dāng)前目錄初始化m_szInitDir getcwd(m_szInitDir,_MAX_PATH); //如果目錄的最后一個(gè)字母不是'\',則在最后加上一個(gè)'\' int len=strlen(m_szInitDir); if (m_szInitDir[len-1] != '\\') strcat(m_szInitDir,"\\"); } bool CBrowseDir::SetInitDir(const char *dir) { //先把dir轉(zhuǎn)換為絕對(duì)路徑 if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL) return false; //判斷目錄是否存在 if (_chdir(m_szInitDir) != 0) return false; //如果目錄的最后一個(gè)字母不是'\',則在最后加上一個(gè)'\' int len=strlen(m_szInitDir); if (m_szInitDir[len-1] != '\\') strcat(m_szInitDir,"\\"); return true; } bool CBrowseDir::BeginBrowse(const char *filespec) { ProcessDir(m_szInitDir,NULL); return BrowseDir(m_szInitDir,filespec); } bool CBrowseDir::BrowseDir(const char *dir,const char *filespec) { _chdir(dir); //首先查找dir中符合要求的文件 long hFile; _finddata_t fileinfo; if ((hFile=_findfirst(filespec,&fileinfo)) != -1) { do { //檢查是不是目錄 //如果不是,則進(jìn)行處理 if (!(fileinfo.attrib & _A_SUBDIR)) { char filename[_MAX_PATH]; strcpy(filename,dir); strcat(filename,fileinfo.name); cout << filename << endl; if (!ProcessFile(filename)) return false; } } while (_findnext(hFile,&fileinfo) == 0); _findclose(hFile); } //查找dir中的子目錄 //因?yàn)樵谔幚韉ir中的文件時(shí),派生類(lèi)的ProcessFile有可能改變了 //當(dāng)前目錄,因此還要重新設(shè)置當(dāng)前目錄為dir。 //執(zhí)行過(guò)_findfirst后,可能系統(tǒng)記錄下了相關(guān)信息,因此改變目錄 //對(duì)_findnext沒(méi)有影響。 _chdir(dir); if ((hFile=_findfirst("*.*",&fileinfo)) != -1) { do { //檢查是不是目錄 //如果是,再檢查是不是 . 或 .. //如果不是,進(jìn)行迭代 if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name,".") != 0 && strcmp (fileinfo.name,"..") != 0) { char subdir[_MAX_PATH]; strcpy(subdir,dir); strcat(subdir,fileinfo.name); strcat(subdir,"\\"); ProcessDir(subdir,dir); if (!BrowseDir(subdir,filespec)) return false; } } } while (_findnext(hFile,&fileinfo) == 0); _findclose(hFile); } return true; } bool CBrowseDir::ProcessFile(const char *filename) { return true; } void CBrowseDir::ProcessDir(const char *currentdir,const char *parentdir) { } //從CBrowseDir派生出的子類(lèi),用來(lái)統(tǒng)計(jì)目錄中的文件及子目錄個(gè)數(shù) class CStatDir:public CBrowseDir { protected: int m_nFileCount; //保存文件個(gè)數(shù) int m_nSubdirCount; //保存子目錄個(gè)數(shù) public: //缺省構(gòu)造器 CStatDir() { //初始化數(shù)據(jù)成員m_nFileCount和m_nSubdirCount m_nFileCount=m_nSubdirCount=0; } //返回文件個(gè)數(shù) int GetFileCount() { return m_nFileCount; } //返回子目錄個(gè)數(shù) int GetSubdirCount() { //因?yàn)檫M(jìn)入初始目錄時(shí),也會(huì)調(diào)用函數(shù)ProcessDir, //所以減1后才是真正的子目錄個(gè)數(shù)。 return m_nSubdirCount-1; } protected: //覆寫(xiě)虛函數(shù)ProcessFile,每調(diào)用一次,文件個(gè)數(shù)加1 virtual bool ProcessFile(const char *filename) { m_nFileCount++; return CBrowseDir::ProcessFile(filename); } //覆寫(xiě)虛函數(shù)ProcessDir,每調(diào)用一次,子目錄個(gè)數(shù)加1 virtual void ProcessDir (const char *currentdir,const char *parentdir) { m_nSubdirCount++; CBrowseDir::ProcessDir(currentdir,parentdir); } }; void main() { //獲取目錄名 char buf[256]; printf("請(qǐng)輸入要統(tǒng)計(jì)的目錄名:"); gets(buf); //構(gòu)造類(lèi)對(duì)象 CStatDir statdir; //設(shè)置要遍歷的目錄 if (!statdir.SetInitDir(buf)) { puts("目錄不存在。"); return; } //開(kāi)始遍歷 statdir.BeginBrowse("*.*"); printf("文件總數(shù): %d\n子目錄總數(shù):%d\n",statdir.GetFileCount(),statdir.GetSubdirCount()); }
已在windows上驗(yàn)證有效。
下面我加了BeginBrowseFilenames函數(shù),以vector<char*>形式返回目錄中所有文件名。
實(shí)例代碼:
#include "stdlib.h" #include "direct.h" #include "string.h" #include "string" #include "io.h" #include "stdio.h" #include <vector> #include "iostream" using namespace std; class CBrowseDir { protected: //存放初始目錄的絕對(duì)路徑,以'\'結(jié)尾 char m_szInitDir[_MAX_PATH]; public: //缺省構(gòu)造器 CBrowseDir(); //設(shè)置初始目錄為dir,如果返回false,表示目錄不可用 bool SetInitDir(const char *dir); //開(kāi)始遍歷初始目錄及其子目錄下由filespec指定類(lèi)型的文件 //filespec可以使用通配符 * ?,不能包含路徑。 //如果返回false,表示遍歷過(guò)程被用戶中止 bool BeginBrowse(const char *filespec); vector<string> BeginBrowseFilenames(const char *filespec); protected: //遍歷目錄dir下由filespec指定的文件 //對(duì)于子目錄,采用迭代的方法 //如果返回false,表示中止遍歷文件 bool BrowseDir(const char *dir,const char *filespec); vector<string> GetDirFilenames(const char *dir,const char *filespec); //函數(shù)BrowseDir每找到一個(gè)文件,就調(diào)用ProcessFile //并把文件名作為參數(shù)傳遞過(guò)去 //如果返回false,表示中止遍歷文件 //用戶可以覆寫(xiě)該函數(shù),加入自己的處理代碼 virtual bool ProcessFile(const char *filename); //函數(shù)BrowseDir每進(jìn)入一個(gè)目錄,就調(diào)用ProcessDir //并把正在處理的目錄名及上一級(jí)目錄名作為參數(shù)傳遞過(guò)去 //如果正在處理的是初始目錄,則parentdir=NULL //用戶可以覆寫(xiě)該函數(shù),加入自己的處理代碼 //比如用戶可以在這里統(tǒng)計(jì)子目錄的個(gè)數(shù) virtual void ProcessDir(const char *currentdir,const char *parentdir); }; CBrowseDir::CBrowseDir() { //用當(dāng)前目錄初始化m_szInitDir getcwd(m_szInitDir,_MAX_PATH); //如果目錄的最后一個(gè)字母不是'\',則在最后加上一個(gè)'\' int len=strlen(m_szInitDir); if (m_szInitDir[len-1] != '\\') strcat(m_szInitDir,"\\"); } bool CBrowseDir::SetInitDir(const char *dir) { //先把dir轉(zhuǎn)換為絕對(duì)路徑 if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL) return false; //判斷目錄是否存在 if (_chdir(m_szInitDir) != 0) return false; //如果目錄的最后一個(gè)字母不是'\',則在最后加上一個(gè)'\' int len=strlen(m_szInitDir); if (m_szInitDir[len-1] != '\\') strcat(m_szInitDir,"\\"); return true; } vector<string> CBrowseDir::BeginBrowseFilenames(const char *filespec) { ProcessDir(m_szInitDir,NULL); return GetDirFilenames(m_szInitDir,filespec); } bool CBrowseDir::BeginBrowse(const char *filespec) { ProcessDir(m_szInitDir,NULL); return BrowseDir(m_szInitDir,filespec); } bool CBrowseDir::BrowseDir(const char *dir,const char *filespec) { _chdir(dir); //首先查找dir中符合要求的文件 long hFile; _finddata_t fileinfo; if ((hFile=_findfirst(filespec,&fileinfo)) != -1) { do { //檢查是不是目錄 //如果不是,則進(jìn)行處理 if (!(fileinfo.attrib & _A_SUBDIR)) { char filename[_MAX_PATH]; strcpy(filename,dir); strcat(filename,fileinfo.name); cout << filename << endl; if (!ProcessFile(filename)) return false; } } while (_findnext(hFile,&fileinfo) == 0); _findclose(hFile); } //查找dir中的子目錄 //因?yàn)樵谔幚韉ir中的文件時(shí),派生類(lèi)的ProcessFile有可能改變了 //當(dāng)前目錄,因此還要重新設(shè)置當(dāng)前目錄為dir。 //執(zhí)行過(guò)_findfirst后,可能系統(tǒng)記錄下了相關(guān)信息,因此改變目錄 //對(duì)_findnext沒(méi)有影響。 _chdir(dir); if ((hFile=_findfirst("*.*",&fileinfo)) != -1) { do { //檢查是不是目錄 //如果是,再檢查是不是 . 或 .. //如果不是,進(jìn)行迭代 if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name,".") != 0 && strcmp (fileinfo.name,"..") != 0) { char subdir[_MAX_PATH]; strcpy(subdir,dir); strcat(subdir,fileinfo.name); strcat(subdir,"\\"); ProcessDir(subdir,dir); if (!BrowseDir(subdir,filespec)) return false; } } } while (_findnext(hFile,&fileinfo) == 0); _findclose(hFile); } return true; } vector<string> CBrowseDir::GetDirFilenames(const char *dir,const char *filespec) { _chdir(dir); vector<string>filename_vector; filename_vector.clear(); //首先查找dir中符合要求的文件 long hFile; _finddata_t fileinfo; if ((hFile=_findfirst(filespec,&fileinfo)) != -1) { do { //檢查是不是目錄 //如果不是,則進(jìn)行處理 if (!(fileinfo.attrib & _A_SUBDIR)) { char filename[_MAX_PATH]; strcpy(filename,dir); strcat(filename,fileinfo.name); filename_vector.push_back(filename); } } while (_findnext(hFile,&fileinfo) == 0); _findclose(hFile); } //查找dir中的子目錄 //因?yàn)樵谔幚韉ir中的文件時(shí),派生類(lèi)的ProcessFile有可能改變了 //當(dāng)前目錄,因此還要重新設(shè)置當(dāng)前目錄為dir。 //執(zhí)行過(guò)_findfirst后,可能系統(tǒng)記錄下了相關(guān)信息,因此改變目錄 //對(duì)_findnext沒(méi)有影響。 _chdir(dir); if ((hFile=_findfirst("*.*",&fileinfo)) != -1) { do { //檢查是不是目錄 //如果是,再檢查是不是 . 或 .. //如果不是,進(jìn)行迭代 if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name,".") != 0 && strcmp (fileinfo.name,"..") != 0) { char subdir[_MAX_PATH]; strcpy(subdir,dir); strcat(subdir,fileinfo.name); strcat(subdir,"\\"); ProcessDir(subdir,dir); vector<string>tmp= GetDirFilenames(subdir,filespec); for (vector<string>::iterator it=tmp.begin();it<tmp.end();it++) { filename_vector.push_back(*it); } } } } while (_findnext(hFile,&fileinfo) == 0); _findclose(hFile); } return filename_vector; } bool CBrowseDir::ProcessFile(const char *filename) { return true; } void CBrowseDir::ProcessDir(const char *currentdir,const char *parentdir) { } //從CBrowseDir派生出的子類(lèi),用來(lái)統(tǒng)計(jì)目錄中的文件及子目錄個(gè)數(shù) class CStatDir:public CBrowseDir { protected: int m_nFileCount; //保存文件個(gè)數(shù) int m_nSubdirCount; //保存子目錄個(gè)數(shù) public: //缺省構(gòu)造器 CStatDir() { //初始化數(shù)據(jù)成員m_nFileCount和m_nSubdirCount m_nFileCount=m_nSubdirCount=0; } //返回文件個(gè)數(shù) int GetFileCount() { return m_nFileCount; } //返回子目錄個(gè)數(shù) int GetSubdirCount() { //因?yàn)檫M(jìn)入初始目錄時(shí),也會(huì)調(diào)用函數(shù)ProcessDir, //所以減1后才是真正的子目錄個(gè)數(shù)。 return m_nSubdirCount-1; } protected: //覆寫(xiě)虛函數(shù)ProcessFile,每調(diào)用一次,文件個(gè)數(shù)加1 virtual bool ProcessFile(const char *filename) { m_nFileCount++; return CBrowseDir::ProcessFile(filename); } //覆寫(xiě)虛函數(shù)ProcessDir,每調(diào)用一次,子目錄個(gè)數(shù)加1 virtual void ProcessDir (const char *currentdir,const char *parentdir) { m_nSubdirCount++; CBrowseDir::ProcessDir(currentdir,parentdir); } }; void main() { //獲取目錄名 char buf[256]; printf("請(qǐng)輸入要統(tǒng)計(jì)的目錄名:"); gets(buf); //構(gòu)造類(lèi)對(duì)象 CStatDir statdir; //設(shè)置要遍歷的目錄 if (!statdir.SetInitDir(buf)) { puts("目錄不存在。"); return; } //開(kāi)始遍歷 vector<string>file_vec = statdir.BeginBrowseFilenames("*.*"); for(vector<string>::const_iterator it = file_vec.begin(); it < file_vec.end(); ++it) std::cout<<*it<<std::endl; printf("文件總數(shù): %d\n",file_vec.size()); system("pause"); }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- C++遍歷文件夾獲取文件列表
- 一波二叉樹(shù)遍歷問(wèn)題的C++解答實(shí)例分享
- C++遍歷文件夾下文件的方法
- C++遍歷Lua table的方法實(shí)例
- C++實(shí)現(xiàn)圖的鄰接表存儲(chǔ)和廣度優(yōu)先遍歷實(shí)例分析
- C++實(shí)現(xiàn)圖的鄰接矩陣存儲(chǔ)和廣度、深度優(yōu)先遍歷實(shí)例分析
- C++實(shí)現(xiàn)二叉樹(shù)遍歷序列的求解方法
- C++實(shí)現(xiàn)哈夫曼樹(shù)簡(jiǎn)單創(chuàng)建與遍歷的方法
- C++非遞歸遍歷磁盤(pán)文件和遞歸遍歷磁盤(pán)文件的程序示例
- 探討:C++實(shí)現(xiàn)鏈?zhǔn)蕉鏄?shù)(用非遞歸方式先序,中序,后序遍歷二叉樹(shù))
- c++二叉樹(shù)的幾種遍歷算法
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C語(yǔ)言創(chuàng)建鏈表錯(cuò)誤之通過(guò)指針參數(shù)申請(qǐng)動(dòng)態(tài)內(nèi)存實(shí)例分析
這篇文章主要介紹了C語(yǔ)言創(chuàng)建鏈表錯(cuò)誤之通過(guò)指針參數(shù)申請(qǐng)動(dòng)態(tài)內(nèi)存,是鏈表創(chuàng)建過(guò)程中非常常見(jiàn)的經(jīng)典錯(cuò)誤。實(shí)例中做了較為詳盡的分析,需要的朋友可以參考下2014-09-09Ubuntu中使用VS Code與安裝C/C++插件的教程詳解
這篇文章主要介紹了Ubuntu中使用VS Code與安裝C/C++插件的教程詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Linux?C/C++?timeout命令實(shí)現(xiàn)運(yùn)行具有時(shí)間限制功能
inux?timeout命令的一個(gè)屬性是時(shí)間限制??梢詾槿魏蚊钤O(shè)置時(shí)間限制。如果時(shí)間到期,命令將停止執(zhí)行,這篇文章主要介紹了Linux?C/C++?timeout命令實(shí)現(xiàn)(運(yùn)行具有時(shí)間限制),需要的朋友可以參考下2023-02-02LeetCode 單調(diào)棧內(nèi)容小結(jié)
這篇文章主要介紹了LeetCode 單調(diào)棧內(nèi)容小結(jié),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++編寫(xiě)簡(jiǎn)易的飛機(jī)大戰(zhàn)
一款自己設(shè)計(jì)的飛機(jī)小游戲,本程序于運(yùn)行環(huán)境WINDOWS XP系統(tǒng),采用C++語(yǔ)言編寫(xiě)。游戲具有得分排名榜,而且在游戲完成后可以提交得分到網(wǎng)絡(luò)上的世界排名榜中。2015-08-08C 語(yǔ)言關(guān)于聯(lián)合體的相關(guān)知識(shí)
這篇文章主要介紹了C 語(yǔ)言關(guān)于聯(lián)合體的相關(guān)知識(shí),文中講解非常細(xì)致,代碼幫助大家更好的理解學(xué)習(xí),感興趣的朋友可以了解下2020-06-06Qt QChart 創(chuàng)建圖表的實(shí)現(xiàn)方法
這篇文章主要介紹了Qt QChart 創(chuàng)建圖表的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12