linux 系统编程目录操作,嵌入式 Linux系统编程(五)——目录文件函数

嵌入式 Linux系统编程(五)——目录文件函数

Linux中目录也是文件,目录操作函数为标准IO库函数。主要函数如下:

#include

#include

DIR *opendir(const char *name);

DIR *fdopendir(int fd);

成功返回一个指向目录流的指针,失败返回NULL,并且设置errno全局变量。

#include

struct dirent *readdir(DIR *dirp);

成功返回一个指向目录 dirent结构的指针,如果到达目录流结尾或错误返回NULL。

#include

int scandir(const char *dirp,//目录名

struct dirent ***namelist,//返回目录列表

int (*filter)(const struct dirent *),//过滤目录,NULL不过滤

int (*compar)(const struct dirent **,const struct dirent **));//排序返回目录,NULL不排序

成功返回目录内文件的数量,失败返回-1。

目录文件信息结构体dirent:

struct dirent {

ino_t          d_ino;       /* inode number */

off_t          d_off;       /* offset to the next dirent */

unsigned short d_reclen;    /* length of this record */

unsigned char  d_type;      /* type of file; not supported

by all file system types */

char           d_name[256]; /* filename */

};

遍历目录函数:

int traverse_dir(const char *path)

{

struct dirent **dent;

unsigned int i = 0;

i = scandir(path, &dent, NULL, NULL);

while(*dent)

{

printf("%s\n", (*dent)->d_name);

dent++;

}

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值