Linux C中readdir()函数:读取目录函数

239 篇文章 12 订阅

头文件:#include <sys/types.h>   #include <dirent.h>
函数原型:struct dirent * readdir(DIR * dir);
函数功能:readdir()返回参数dir 目录流的下个目录进入点。结构dirent 定义如下:
             struct dirent
             {
             ino_t d_ino;                         //d_ino 此目录进入点的inode
             ff_t d_off;                             //d_off 目录文件开头至此目录进入点的位移
             signed short int d_reclen;  //d_reclen _name 的长度, 不包含NULL 字符
             unsigned char d_type;       //d_type d_name 所指的文件类型
             har d_name[256];              //d_name 文件名
             };

            补充文件类型:p:1   c:2  d:4  b:6  f:8  l:10

返回值:成功则返回下个目录进入点. 有错误发生或读取到目录文件尾则返回NULL.

范例
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
main()
{
    DIR * dir;
    struct dirent * ptr;
    int i;
    dir = opendir("/etc/rc.d");
    while((ptr = readdir(dir)) != NULL)
    {
        printf("d_name : %s\n", ptr->d_name);
    }
    closedir(dir);
}
执行:
d_name : .
d_name : ..
d_name : 1.c
d_name : 2.c
d_name : app
d_name : 3.c
————————————————
版权声明:本文为CSDN博主「丶Apache」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35769746/article/details/81284546

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值