遍历与升压::文件系统的目录没有抛出异常

编程入门 行业动态 更新时间:2024-10-21 19:48:24
本文介绍了遍历与升压::文件系统的目录没有抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要的目录的路径,我想通过它的所有子目录的遍历,顺便收集文件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.

更多推荐

遍历与升压::文件系统的目录没有抛出异常

本文发布于:2023-05-28 06:55:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/315104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   抛出   文件系统   异常   目录

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!