- 论坛徽章:
- 0
|
本帖最后由 wkx9dragon 于 2011-10-19 16:15 编辑
我的目的是遍历指定目录下所有文件,包括子目录,如果某个目录禁止访问,则跳过。
一直想找个能够处理禁止访问目录的,遍历函数,boost给我了希望,但是现在卡壳了,需要大家的帮忙,谢谢了。
//
namespace fs = boost::filesystem;
...
//
void pathList(const string& path, Container& result,unsigned long maxdepth){
using namespace fs;
try{
fs::path p(path);
if (p.empty()) return;
if (fs::is_directory(p)) {
fs::recursive_directory_iterator end_iter;
fs::recursive_directory_iterator itr(p);
label:
while (itr != end_iter) {
result.push_back(itr->path().string());
if( fs::is_directory(*itr) ){
try{
fs::directory_iterator ditr(*itr);
}
catch( const filesystem_error& e){
clog<<__LINE__<<":"<<e.what()<<"\n";
itr.pop();
clog<<__LINE__<<":"<<itr->path().string()<<"\n";
goto label;
}
}
if( itr.level() < maxdepth ) ++itr;
}
}
}
catch( const filesystem_error& e){
clog<<__LINE__<<":"<<e.what()<<"\n";
}
} |
|