nftw,ftw

参考:http://blog.sina.com.cn/s/blog_a11fcae9010141l8.html

http://www.man7.org/linux/man-pages/man3/ftw.3.html

ftw:意思是文件树遍历。file tree walk.


 #include <ftw.h>

       int nftw(const char *dirpath,
                int (*fn) (const char *fpath, const struct stat *sb,int typeflag, struct FTW *ftwbuf),                 		int nopenfd, int flags);

       #include <ftw.h>

       int ftw(const char *dirpath,
               int (*fn) (const char *fpath, const struct stat *sb,int typeflag),                          
               int nopenfd);

 struct FTW {//这个结构体,极有可能在 <ftw.h>这个头文件中没有,我们可以自己定义一下。
               int base;//这个值,指的是目录名的长度,不同的字符编码,可能有区别,需要测试比如中文目录时
               int level;//当前扫描的目录的级别,0级是指dirpath,依此累加
           };
 
int typeflag这个参数有以下几种值:
FTW_F  fpath is a regular file.表示普通文件
FTW_D  fpath is a directory.目录
FTW_DNR fpath is a directory which can't be read 不可读的目录
FTW_DP fpath is a directory, and FTW_DEPTH was specified in flags.
              (If FTW_DEPTH was not specified in flags, then directories
              will always be visited with typeflag set to FTW_D.)  All of
              the files and subdirectories within fpath have been processed.所有的文件和子目录都被处理过了。
FTW_NS The stat(2) call failed on fpath, which is not a symbolic
              link.  The probable cause for this is that the caller had read
              permission on the parent directory, so that the filename fpath
              could be seen, but did not have execute permission, so that
              the file could not be reached for stat(2).对fpath进行stat时失败,但fpath不是符号连接,导致这种情况的可能原因是,函数调用者有读取父目录的权限,所以这个文件名fpath对调用者是可见的,但不具备执行权限,所以stat时不可以获取信息失败

FTW_SL  fpath is a symbolic link, and FTW_PHYS was set in flags.符号连接

FTW_SLN fpath is a symbolic link pointing to a nonexistent file.符号连接,但是指向的源文件已不存在
              (This occurs only if FTW_PHYS is not set.)


Because nftw() uses dynamic data structures, the only safe way to
       exit out of a tree walk is to return a nonzero value from fn().  To
       allow a signal to terminate the walk without causing a memory leak,
       have the handler set a global flag that is checked by fn().  Don't
       use longjmp(3) unless the program is going to terminate.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值