相关函数:open, opendir, closedir, rewinddir, seekdir, telldir, scandir

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

#include <dirent.h>

定义函数:struct dirent * readdir(DIR * dir);

函数说明:readdir()返回参数dir 目录流的下个目录进入点。结构dirent 定义如下:

struct dirent
{ino_t d_ino; //d_ino 此目录进入点的inodeff_t d_off; //d_off 目录文件开头至此目录进入点的位移signed short int d_reclen; //d_reclen _name 的长度, 不包含NULL 字符unsigned char d_type; //d_type d_name 所指的文件类型 d_name 文件名har d_name[256];
};

d_type的值为:

  • DT_BLK This is a block device.
  • DT_CHR This is a character device.
  • DT_DIR This is a directory.
  • DT_FIFO This is a named pipe (FIFO).
  • DT_LNK This is a symbolic link.
  • DT_REG This is a regular file.
  • DT_SOCK This is a UNIX domain socket.
  • DT_UNKNOWN The file type is unknown.

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

附加说明:EBADF 参数dir 为无效的目录流。

注意:每次使用readdir后,readdir会读到下一个文件,readdir是依次读出目录中的所有文件,每次只能读一个
范例

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
int main(int argc , char* argv[])
{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);return 0;
}

执行:

d_name : .
d_name : ..
d_name : init.d
d_name : rc0.d
d_name : rc1.d
d_name : rc2.d
d_name : rc3.d
d_name : rc4.d
d_name : rc5.d
d_name : rc6.d
d_name : rc
d_name : rc.local
d_name : rc.sysinit

C语言readdir()函数:读取目录函数相关推荐

  1. linux c readdir 顺序,Linux读取目录函数readdir以及inode结构简介

    readdir 语法: struct dirent* readdir(DIR* dir_handle); 返回值: dirent结构 函数种类: 文件存取 内容说明 本函数用来读取目录.返回目录中的文 ...

  2. c语言readdir函数功能,C语言readdir()函数:读取目录函数

    头文件:#include #include 定义函数:struct dirent * readdir(DIR * dir); 返回值:成功则返回下个目录进入点. 有错误发生或读取到目录文件尾则返回NU ...

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

    头文件:#include <sys/types.h>   #include <dirent.h> 函数原型:struct dirent * readdir(DIR * dir) ...

  4. readdir()函数:读取目录函数

    1,头文件: #include <sys/types.h> #include <dirent.h> 2,定义函数: struct dirent * readdir(DIR * ...

  5. Dart —— 函数 函数参数 匿名函数

    Dart汇总请点击这里 函数 文章目录 函数 可选参数 命名可选参数 位置可选参数 默认参数值 main() 函数 函数是一等对象 匿名函数 返回值 Dart 是一门真正面向对象的语言, 甚至其中的函 ...

  6. PHP目录函数 readdir (从目录句柄中读取条目)

    在PHP中,目录函数 readdir() 用于从目录句柄中读取条目.     函数语法: string readdir ([ resource $dir_handle ] ) 函数参数说明: 参数 描 ...

  7. php seekdir,C++_详解C语言中telldir()函数和seekdir()函数的用法,C语言telldir()函数:取得目录流 - phpStudy...

    详解C语言中telldir()函数和seekdir()函数的用法 C语言telldir()函数:取得目录流的读取位置头文件: #include 定义函数: off_t telldir(DIR *dir ...

  8. c语言实现新建目录函数,C语言中改变目录的相关操作函数详解

    C语言fchdir()函数:改变当前工作目录头文件: #include 定义函数: int fchdir(int fd); 函数说明:fchdir()用来将当前的工作目录改变成以参数fd 所指的文件描 ...

  9. R语言使用haven包的read_spss函数读取spss格式数据、使用haven包的read_sas函数读取SAS格式数据、使用haven包的read_dta函数读取Stata格式数据

    R语言使用haven包的read_spss函数读取spss格式数据.使用haven包的read_sas函数读取SAS格式数据.使用haven包的read_dta函数读取Stata格式数据 目录

最新文章

  1. cpp遇到问题:include重复包含导致的redefinition
  2. 好程序员分享大势所趋 HTML5成Web开发者最关心的技术
  3. js动态给按钮赋id_如何给SHOPIFY店铺添加“立即购买”动态结账按钮
  4. MATLAB教程(1) MATLAB 基础知识(2)
  5. 同步与阻塞的区别与联系
  6. 文件读取 linux_救命,Linux正在吃掉我的内存
  7. 最大期望算法 (EM算法)
  8. 20 Alarms, sigaction(), and Reentrant System Calls
  9. python datasets 下载_frds:金融学术研究数据的Python库
  10. [swift] LeetCode 96. Unique Binary Search Trees
  11. FISCO BCOS 《新摩登时代》:卓别林演绎共识与同步流程优化
  12. ENVI中辐射定标和大气校正
  13. MacBook入门之——添加打印机
  14. python怎么打希腊字母_Python--画图时希腊字母的显示
  15. vscode 管理员权限 运行终端
  16. (SEED-Lab) Sniffing_Spoofing
  17. CIO40: 2020年IT人员如何借机破局
  18. 捡了西瓜丢芝麻——注意编程细节
  19. Java代理模式(Cglib)代理模式
  20. 三国志战略版:Daniel_平民福音-“黑科技阵法”三势阵

热门文章

  1. 从苏宁电器到卡巴斯基(第二部)第02篇:我在卡巴的日子 II
  2. EasyPoiUtil纯代码生成excel进行下载
  3. 四.Redis学习:特殊数据类型
  4. 用到的Android studio依赖库
  5. 数据结构与算法---均摊时间复杂度
  6. 抖音小店无货源前期开店需要多少资金?多久能回本?
  7. windows10中Office2016自定义安装、visio的安装,并安装到其他盘的方法
  8. 怎样恢复sd卡数据?靠这几个方法
  9. DQN相关知识总结及演员-评论员算法介绍(DataWhale组队学习笔记)
  10. 华为鸿蒙ota升级,华为鸿蒙系统官方,华为鸿蒙系统官方升级名单预约 v2.0 - 浏览器家园...