linux目录相关函数opendir、readdir、rewinddir、closedir、telldir、seekdir、stat函数和DIR、dirent、stat结构体详解

一、opendir()

头  文  件:#include<sys/types.h>

                 #include<dirent.h>

函数原型:DIR* opendir (constchar * path );

功       能:打开一个目录

返  回  值:成功则返回DIR*型态的目录流, 打开失败则返回NULL.

DIR结构体:

  1. struct __dirstream
  2. {
  3.       void       *__fd;
  4.       char         *__data;
  5.       int       __entry_data;
  6.       char     *__ptr;
  7.       int         __entry_ptr;
  8.       size_t __allocation;
  9.       size_t __size;
  10.       __libc_lock_define (, __lock)
  11. };
  12. typedef struct __dirstream DIR;


此结构体一般被以下函数使用:
  1. struct dirent *readdir(DIR *dp);
  2. void rewinddir(DIR *dp);
  3. int closedir(DIR *dp);
  4. long telldir(DIR *dp);
  5. void seekdir(DIR *dp,long loc);


二、readdir()
头  文  件: #include<sys/types.h>
                 #include<dirent.h>
函数原型: struct dirent* readdir(DIR* dp);        (需要循环读取dp中的文件和目录,每读
                                                                                 个文件或目录都返回一个dirent结构体指针)
功       能: 读取目录
返  回  值: dirent 结构体指针

dirent结构体:
  1. struct dirent
  2. {
  3.   long d_ino;                        /* inode number 索引节点号 */
  4.     off_t d_off;                       /* offset to this dirent 在目录文件中的偏移 */
  5.     unsigned short d_reclen; /* length of this d_name 文件名长 */
  6.     unsigned char d_type;     /* the type of d_name 文件类型 */
  7.     char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
  8. }


三、rewinddir()
头  文  件: #include <sys/types.h>
                 #include <dirent.h>
函数原型: void  rewinddir(DIR *dp);
功       能: 用来设置参数dp目录流目前的读取位置为原来开头的读取位置

四、closedir()
头  文  件: #include <sys/types.h>
                 #include <dirent.h>
函数原型:int closedir(DIR*dp);
功 能: 关闭参数 dp 所指的目录流
返 回 值: 关闭成功则返回0,,失败返回-1

五、telldir()
头  文  件: #include <sys/types.h>
                 #include <dirent.h>
函数原型:long inttelldir(DIR *dp);
功       能: 获取当前 dp 位置
返  回  值: 返回目录流dp的当前位置, 此返回值代表距离目录文件开头的 偏移量, 有错误发生时返回-1

六、seekdir()
头  文  件: #include <sys/types.h>
                 #include <dirent.h>
函数原型:void seekdir(DIR *dp,long int loc);
功       能: 用来设置参数 dp 目录流当前的读取位置,在调用 readdir() 时便从此新位置开始读取。 参数loc代表距离目录文件开头的偏移量。

七、stat()
头  文  件: #include <sys/stat.h>
                 #include <unistd.h>
函数原型:int stat(const char *file_name,struct stat *buf);
功       能: 通过文件名 file_name 获取文件信息,并保存在 buf 所指的结构体 stat
返  回  值: 执行成功则返回0,失败返回-1

stat结构体:
  1. struct stat {
  2.         mode_t     st_mode;       //文件访问权限
  3.         ino_t      st_ino;       //索引节点号
  4.         dev_t      st_dev;        //文件使用的设备号
  5.         dev_t      st_rdev;       //设备文件的设备号
  6.         nlink_t    st_nlink;      //文件的硬连接数
  7.         uid_t      st_uid;        //所有者用户识别号
  8.         gid_t      st_gid;        //组识别号
  9.         off_t      st_size;       //以字节为单位的文件容量
  10.         time_t     st_atime;      //最后一次访问该文件的时间
  11.         time_t     st_mtime;      //最后一次修改该文件的时间
  12.         time_t     st_ctime;      //最后一次改变该文件状态的时间
  13.         blksize_t st_blksize;    //包含该文件的磁盘块的大小
  14.         blkcnt_t   st_blocks;     //该文件所占的磁盘块
  15.       };
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值