C語言實(shí)現(xiàn)遍歷文件夾中的文件
文件目錄如下,文件夾里還有一些txt文件未展示出來。


使用遞歸實(shí)現(xiàn),深度優(yōu)先遍歷文件夾中的文件。
代碼如下,用了一點(diǎn)C++的語法。
#include <io.h>
#include <iostream>
using namespace std;
#define MAX_PATH_LENGTH 100
int Traverse(char dir[]);
int main()
{
char dir[MAX_PATH_LENGTH] = "e:\\test\\*.*";
Traverse(dir);
return 0;
}
int Traverse(char dir[])
{
intptr_t handle;
_finddata_t findData;
handle = _findfirst(dir, &findData);
if (handle == -1)
{
cout << "no file exsit\n";
return -1;
}
do
{
if ((findData.attrib & _A_SUBDIR) && (strcmp(findData.name, ".") != 0) && (strcmp(findData.name, "..") != 0))
{
//it is a directory
cout << "subdir:" << findData.name << endl;
char sub_dir[MAX_PATH_LENGTH] = { 0 };
string s(dir);
sprintf_s(sub_dir, "%s%s\\*.*", s.substr(0, s.length() - 3).c_str(), findData.name);
Traverse(sub_dir);
}
else if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
{
//it is . or .. , do nothing
}
else
{
//it is a file
cout << "file:" << findData.name << endl;
}
} while (_findnext(handle, &findData) == 0);
_findclose(handle);
}運(yùn)行結(jié)果如下:

到此這篇關(guān)于C語言實(shí)現(xiàn)遍歷文件夾中的文件的文章就介紹到這了,更多相關(guān)C語言遍歷文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)斐波那契數(shù)列(非遞歸)的實(shí)例講解
下面小編就為大家?guī)硪黄狢語言實(shí)現(xiàn)斐波那契數(shù)列(非遞歸)的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
c++調(diào)用python實(shí)現(xiàn)圖片ocr識別
所謂c++調(diào)用python,實(shí)際上就是在c++中把整個(gè)python當(dāng)作一個(gè)第三方庫引入,然后使用特定的接口來調(diào)用python的函數(shù)或者直接執(zhí)行python腳本,本文介紹的是調(diào)用python實(shí)現(xiàn)圖片ocr識別,感興趣的可以了解下2023-09-09
C++語言基礎(chǔ) this和static關(guān)鍵字
這篇文章主要介紹了C++語言基礎(chǔ) this和static關(guān)鍵字,需要的朋友可以參考下2020-01-01
C++設(shè)置系統(tǒng)時(shí)間及系統(tǒng)時(shí)間網(wǎng)絡(luò)更新的方法
這篇文章主要介紹了C++設(shè)置系統(tǒng)時(shí)間及系統(tǒng)時(shí)間網(wǎng)絡(luò)更新的方法,涉及網(wǎng)絡(luò)程序設(shè)計(jì)與系統(tǒng)函數(shù)的使用,需要的朋友可以參考下2014-10-10
C++算法之在無序數(shù)組中選擇第k小個(gè)數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++算法之在無序數(shù)組中選擇第k小個(gè)數(shù)的實(shí)現(xiàn)方法,涉及C++數(shù)組的遍歷、判斷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-03-03

