|
在一个目录流 dirp 中, readdir 函数返回一个指向描述下一个目录条目的结的指针.连续调用 readdir则返回更多的目录条目.若错误和到了目录末尾,readdir就会返回 NULL.在 POSIX 兼容系统(POSIX-compliant)里,如果是因为在目录末尾而返回 NULL的系统不会改变 errno的值,但如果是因为错误而返回 NULL的,就会设置 errno .
用法:#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
注意,如果有其他的进程同时在创建和删除目录里的文件, readdir 扫描并不保证会列出所有在目录里的文件.
dirent 结构包含的目录条目细节包含下面的2个条目:
ino_t d_ino : 文件的节点
char d_name[] : 文件名
如果要确定更多的目录里文件的细节,需要使用 stat 调用来进行查询.
struct dirent
{
#ifndef __USE_FILE_OFFSET64
__ino_t d_ino;
__off_t d_off;
#else
__ino64_t d_ino;
__off64_t d_off;
#endif
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256]; /* We must not include limits.h! */
}; |
|