深度優先算法遍歷文件夾裏面的各文件

//深度優先遍歷文件夾中各個文件
int dfsFolder(string folderPath, ofstream &fout)
{

_finddata_t FileInfo;
string strfind = folderPath +"\\*";
long Handle = _findfirst(strfind.c_str(), &FileInfo);
if (Handle == -1L)
{
//cerr << "can not match the folder path" << endl;
exit(-1);
}
do{
//判斷是否有子目錄
if (FileInfo.attrib & _A_SUBDIR) 
{
//子目錄
if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0)) 
{
string newPath = folderPath + "\\" + FileInfo.name;
dfsFolder(newPath, fout);
}
}
else 
{
//此處添加對各個文件的操作

}
}while (_findnext(Handle, &FileInfo) == 0);
_findclose(Handle);
fout.close();
return flag;
}
發佈了10 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章