深度优先算法遍历文件夹里面的各文件

//深度优先遍历文件夹中各个文件
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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章