Linux中读取目录: opendir,fdopendir,readdir,rewinddir,closedir,telldir,seekdir

这篇博客介绍了Linux中读取目录的相关函数,如opendir和fdopendir,以及dirent结构体。文章阐述了目录的访问权限和其在不同Unix系统中的实现,并通过示例代码展示了如何使用这些函数遍历文件系统,同时提到了在处理符号链接时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Directories can be read by anyone who has access permission to read the directory. But only the kernel can write to a directory, to preserve file system sanity(心智健康)(只有内核可以写如目录来保证文件系统的正常运作).

目录的写权限和执行权限可以让我们在目录中创建新文件和移除文件。但是并不能让我们写入目录它本身。

目录的实际形式取决于Unix系统实现和文件系统的设计。早期的系统中,使用简单的结构:目录entry有16字节,14byte for filename, 2byte for inode number。随着更长的文件名加入,每个entry都是可变的长度。

为简化读目录操作,POSIX1规定了一系列directory routines。许多实现访问应用程序使用read来访问目录的内容。

opendir, fdopendir - open a directory

#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);
DIR *fdopendir(int fd);
//Return: pointer if OK, NULL on error
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值