我要的目录的路径,我想通过它的所有子目录的遍历,顺便收集文件pathes。
I have a path to the directory, and I want to traverse through all of its sub-directories, collecting files' pathes by the way.
namespace fs = boost::filesystem; std::vector<fs::path> traverse_if_directory(fs::path& f) { std::vector<fs::path> result; if (fs::is_directory(f)) { for (fs::recursive_directory_iterator it(f), eit; it != eit; ++it) { if (!fs::is_directory(it->path())) { result.push_back(it->path()); } } } else { result.push_back(f); } return result; }不幸的是,在横向的中间,我偶然发现一个目录,我没有权利看,并高于code抛出。但很明显,在这种情况下它不是一个例外,我只是去,这种锁定目录跳过。
Unfortunately, in the middle of the traversing, I stumble upon a directory which I have no rights to look at, and the code above throws. But obviously, in this scenario it's not an exception, I should just go on, skipping this locked directory.
但我怎么做到这一点?
But how do I do this?
推荐答案呵呵,想通了,还有一个办法:
Ha, figured it out, there is a way:
std::vector<fs::path> traverse_if_directory(fs::path& f) { std::vector<fs::path> result; boost::system::error_code ec; if (fs::is_directory(f)) { for ( fs::recursive_directory_iterator it(f, ec), eit; it != eit; it.increment(ec) ) { if (ec) { it.pop(); continue; } if (!fs::is_directory(it->path())) { result.push_back(it->path()); } } } else { result.push_back(f); } return result; }有是接受类型的输出参数的非抛过载的boost ::系统::错误_ code ,这样我就可以每次递增之后检查如果有任何错误。
There is a non-throwing overload which accepts an output parameter of type boost::system::error_code, so I can just check after each increment if there were any error.
更多推荐
遍历与升压::文件系统的目录没有抛出异常
发布评论