linux下遍历目录树方法总结

上传人:工**** 文档编号:502597176 上传时间:2023-01-24 格式:DOC 页数:16 大小:65.50KB
返回 下载 相关 举报
linux下遍历目录树方法总结_第1页
第1页 / 共16页
linux下遍历目录树方法总结_第2页
第2页 / 共16页
linux下遍历目录树方法总结_第3页
第3页 / 共16页
linux下遍历目录树方法总结_第4页
第4页 / 共16页
linux下遍历目录树方法总结_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《linux下遍历目录树方法总结》由会员分享,可在线阅读,更多相关《linux下遍历目录树方法总结(16页珍藏版)》请在金锄头文库上搜索。

1、linux 下遍历目录树方法总结linux 下遍历目录树方法总结转自: http:/ 前 几天需要实现对整个目录树的遍历,查阅了相关的一些资料。 开始找到的原始的方法是使用 readdir() 与 lstat() 函数实现递 归遍历,后来发现 linux 对于目录遍历这种最常用的操作已 经提供了很完善的接口: ftw() 与 nftw() 。下面就这两种方法 具体说明一下。 1、手动实现递归 1.1 stat() 函数族 stat 函数 族包括: stat,fstat 以及 lstat 函数, 都是向用户返回文件的属 性信息(元数据) 。#include <sys/stat.h>i

2、nt stat(const char*pathname,struct stat*buf);int fstat(int filedes,struct stat*buf);int lstat(const char *pathname,struct stat*buf);三个函数的返回: 若成功为 0,出错为 -1。对一个 pathname , stat 函数返回一个与此命名文件有关的信息结构, fstat 函数 获得已在描述符 filedes 上打开的文件的有关信息。 lstat 函 数类似于 stat, 但是当命名的文件是一个符号连接时, lstat 返回该符号连接的有关信息,而不是由该符号连接引

3、用的文 件的信息。 第二个参数是个指针,它指向一个我们应提 供的结构。这些函数填写由 buf 指向的结构。该结构的实际 定义可能所实施而有所不同,但其基本形式是:struct statmode st_mode; /* 文件类型和方式 ( 许可数 )*/ino st_ino;/* i- 节点号 (序列号 )*/ dev st_dev;/* 设备号 (文件系统 )*/ dev st_rdev;/* 特殊文件的设备号 */ nlink st_nlink;/* 连接数 */ uid st_uid;/* 属主的用户 ID*/ gid st_gid;/* 属主的组 ID*/ off st_size;/*

4、普通文件的字节长度 */ time st_atime;/* 最后存取时间 */ time st_mtime;/* 最后修改存取时间 */ time st_ctime;/* 最后文件状态更改时间 */ long st_blksize;/* 最佳 I/O 块长 */ long st_blocks;/* 分配的 512 字节块块数 ;下面是一个简单的测试 #include<unistd.h> #include<sys/stat.h> #include<stdio.h>int main(int argc, char *argv)struct stat buf;if(

5、stat(argv1,&buf) printf(stat:error!/n); return -1;printf(st_dev:%d/n,buf.st_dev); printf(st_ino:%d/n,buf.st_ino); printf(st_mode:%dS_ISDIR:%d/n,buf.st_mode,S_ISDIR(buf.st_mode); printf(st_nlink:%d/n,buf.st_nlink); printf(st_uid:%d/n,buf.st_uid); printf(st_gid:%d/n,buf.st_gid); printf(st_rdev:%d/

6、n,buf.st_rdev); printf(st_size:%d/n,buf.st_size); printf(st_blksize:%lu/n,buf.st_blksize); printf(st_blocks:%lu/n,buf.st_blocks); printf(st_atime:%ld/n,buf.st_atime); printf(st_mtime:%ld/n,buf.st_mtime); printf(st_ctime:%ld/n,buf.st_ctime);return 0; 这里补充说明一下 linux 中文件的基本类型。 1. 普通文件 (Regular file) 。这

7、是最常见的文件类型,这种文件包含了某 种形式的数据。至于这种数据是文本还是二进制数据对于系 统核而言并无区别。对普通文件内容的解释由处理该文件的 应用程序进行。2. 目录文件 (Directory file) 。这种文件包含了其它文件的名字 以及指向与这些文件有关信息的指针。对一个目录文件具有 读许可数的任一进程都可以读该目录的内容,但只有系统核 可以写目录文件。3. 字符特殊文件 (Charocter special file) 。这种文件用于系统 中的某些类型的设备。4. 块特殊文件 (Block special file) 。这种文件典型地用于磁盘 设备。系统中的所有设备或者是字符特殊文

8、件,或者是块特 殊文件。5. FIFO 。这种文件用于进程间的通信,有时也将其称为命名 管道。6. 套接口 (socket) 。这种文件用于进程间的网络通信。 套接口 也可用于在一台宿主机上的进程之间的非网络通信。7. 符号连接 (Symboliclink) 。这种文件指向另一个文件。对于 文件类型,可以利用定义的宏比如 S_ISDIR() 等测试 st_mode ,判断文件类型。宏有 S_ISREG 、 S_ISDIR 、 S_ISCHR 、S_ISBLK 、S_ISFIFO 、S_ISLNK 、S_ISSOCK 。1.2 遍历目录例子引用别人的一个例子,现在把许多文件处 理函数集中在一起使

9、用,程序遍历指定目录的文件,同时也 要进到下级子目录中进行遍历,这一点是将子目录递归传递 到 opendir 中去,需要指出的是,这就决定了如果子目录嵌 套过深,程序将失败返回,因为允许打开的子目录流数量是 有上限的。/* We start with the appropriate headers and then a function, printdir,which prints out the current directory.It will recurse for subdirectories, using the depth parameter is used for indenta

10、tion.*/#include <unistd.h>#include <stdio.h>#include <dirent.h>#include <string.h>#include <sys/stat.h>void printdir(char *dir, int depth)DIR *dp;struct dirent *entry;struct stat statbuf;if(dp = opendir(dir) = NULL) fprintf(stderr,cannot open directory: %s/n, dir);retur

11、n;chdir(dir);while(entry = readdir(dp) != NULL) lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode) /*/* Found a directory, but ignore . and . */ if(strcmp(.,entry->d_name) = 0 | strcmp(.,entry->d_name) = 0) continue;printf(%*s%s/n,depth,entry->d_name);/*/* Recurse at a new in

12、dent level */ printdir(entry->d_name,depth+4);else printf(%*s%s/n,depth,entry->d_name);chdir(.);/ This folder is traversedoverclosedir(dp);/*/* Now we move onto the main function.*/int main(int argc, char* argv)char *topdir, pwd2=.;if (argc != 2)topdir=pwd;elsetopdir=argv1;printf(Directory sca

13、n of %s/n,topdir);printdir(topdir,0);printf(done./n);exit(0); 本文来自 CSDN 博客,转载请标明出处: http:/ 9.aspx2 、使用 ftw 调用遍历目录 2.1ftw 函数族 使用 readdir 函数等实现递归遍历目录树的方法比较原始, glibc2.1 收录了 ftw 等函数,可以方便实现目录树的遍历。 view plaincopy to clipboardprint?#include <ftw.h>int ftw(const char *dirpath,int (*fn) (const char *fp

14、ath, const struct stat *sb,int typeflag),int nopenfd);#define _XOPEN_SOURCE 500#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)

15、(const char *fpath, const struct stat *sb,int typeflag),int nopenfd);#define _XOPEN_SOURCE 500#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); 具体的英文解释可以参考文 章 ftw, nftw - file tree walk 。 ftw() 函数说明: ftw() 会从参数 dirpath 指定的目录开始,往下一 层层地递归式遍历子目录。 ftw() 会传三个参数给 fn(), 第一 个参数 *fpath 指向当时所在的目录路径,第二个参数是 *sb, 为 stat 结构指

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号