目录递归 [英] Directory recursion

查看:136
本文介绍了目录递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您需要递归浏览目录树,有两种方法可以执行:

If you need to recursively traverse a directory tree, there are two ways to do it:


  1. 构建路径名增加长度,... / ... / ...等。

  1. Build up pathnames of increasing length as you go, .../.../... etc.

使用chdir下载到每个目录,当你来,所以你永远不会处理超过两个组件的路径名。

Use chdir to step down into each directory as you come to it, so you are never dealing with pathnames longer than two components.

第一种方法使我更明显,对于不正当的事件,如果你在半途而废之前,可能会更强大一些。另一方面,查看GNU find 实用程序的代码,我注意到它使用第二种方法。有原因吗?任何我没有想到的第二种方法的优点?

The first method strikes me as more obvious, and might be more robust against untoward events like something being unmounted while you are halfway through it. On the other hand, looking over the code for the GNU find utility, I notice it uses the second method. Is there a reason for that? Any advantage of the second method that I hadn't thought of?

推荐答案

呃...其实现代化的实现可能会使用

Erm... in fact a modern implementation will likely use the

  • ftw
  • nftw calls

ftw 文件树走路的缩写

另见一个非常有用的资源: http://rosettacode.org/wiki/Walk_a_directory/Recursively#Library:_POSIX

See also a very useful resource: http://rosettacode.org/wiki/Walk_a_directory/Recursively#Library:_POSIX

这篇关于目录递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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