boost :: filesystem :: directory_iterator是否因删除而失效? [英] Is boost::filesystem::directory_iterator invalidated by deletion?

查看:681
本文介绍了boost :: filesystem :: directory_iterator是否因删除而失效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历一个目录,当一个项目符合某些条件时,我将其删除。我可以在循环中安全地执行此操作,还是必须在数组中保存路径并稍后删除?

I am iterating through a directory, and when an item matches some criteria, I delete it. Can I do it safely from within the loop, or to I have to save paths in an array and delete then later?

我在 boost :: filesystem docs 。

推荐答案

引用附加到 boost :: filesystem :: directory_iterator的文档(强调是我自己的):

Quoting the first part of a note attached to the docs of boost::filesystem::directory_iterator (emphasis is my own):


执行目录迭代的程序可能希望测试通过解引用目录迭代器获得的路径是否实际存在。它可能是指向不存在的文件的符号链接。 递归遍历目录树以删除并重命名条目的程序可能希望避免使用符号链接。

Programs performing directory iteration may wish to test if the path obtained by dereferencing a directory iterator actually exists. It could be a symbolic link to a non-existent file. Programs recursively walking directory trees for purposes of removing and renaming entries may wish to avoid following symbolic links.

我很清楚,为了删除文件而迭代目录是官方支持的用例,因此不会使迭代器无效。另外,引用该注释的第二部分:

I becomes clear that iterating a directory for the purpose of removing files is an officially supported use case and therefore will not invalidate the iterator. Also, quoting the second part of that note:


如果在构建directory_iterator之后从文件中删除文件或将其添加到目录中在目录中,未指定迭代器的后续递增是否会导致迭代器的值为已删除或添加的目录条目。请参阅ISO / IEC 9945 readdir_r()。

If a file is removed from or added to a directory after the construction of a directory_iterator for the directory, it is unspecified whether or not subsequent incrementing of the iterator will ever result in an iterator whose value is the removed or added directory entry. See ISO/IEC 9945 readdir_r().

这是一个非常具体的陈述,说明在迭代过程中是否会出现删除的文件一个目录。同样,我理解迭代过程本身在任何情况下都是有效的。

This is a very specific statement about whether or not a removed file will appear while iterating over a directory. Again, I understand that the iteration process itself remains valid in any case.

注意 ISO / IEC 9945 有类似的措辞。

这篇关于boost :: filesystem :: directory_iterator是否因删除而失效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆