std::filesystem::recursive_directory_iterator 异常 [英] The std::filesystem::recursive_directory_iterator exception
问题描述
以下函数:
void foo(const std::string& dir)
{
for (auto& el : std::filesystem::recursive_directory_iterator(dir)) {
std::cout << el.path() << '\n';
}
}
用于:
int main()
{
std::string p = "C:\\";
foo(p);
}
当它到达 Windows 10 上的某个文件夹(我假设)时引发异常.代码是在 VS 2017 上编译的,支持 C++17.异常消息是:
raises an exception when it reaches a certain folder (I assume) on Windows 10. The code is compiled on VS 2017 with C++17 support. The exception message is:
recursive_directory_iterator::operator++: The system cannot find the path specified.
同样的行为在使用 std::filesystem::directory_iterator
时也会发生.为什么会在该特定文件夹上引发异常?
The same behavior occurs when using the std::filesystem::directory_iterator
too.
Why is it raising an exception on that particular folder?
推荐答案
显然,当操作系统拒绝访问其中一个文件夹的权限时会引发异常.
Apparently, exception is raised when the OS denies a permission to access one of the folders.
解决方法是利用适当的递归目录迭代器构造函数重载(第四个)并提供 skip_permission_denied
参数:
The workaround is to utilize the appropriate recursive directory iterator constructor overload (4th one) and supply the skip_permission_denied
parameter:
for (auto& el : std::filesystem::recursive_directory_iterator(dir, std::filesystem::directory_options::skip_permission_denied)) {
std::cout << el.path() << '\n';
}
这篇关于std::filesystem::recursive_directory_iterator 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!