struct dirent 与 DIR
1.存储目录中的文件信息(文件名、扩展名等等)
{
void *__fd; /* `struct hurd_fd' pointer for descriptor. */
char *__data; /* Directory block. */
int __entry_data; /* Entry number `__data' corresponds to. */
char *__ptr; /* Current pointer into the block. */
int __entry_ptr; /* Entry number `__ptr' corresponds to. */
size_t __allocation; /* Space allocated for the block. */
size_t __size; /* Total valid data in the block. */
__libc_lock_define (, __lock) /* Mutex lock for this structure. */
};
typedef struct __dirstream DIR;
telldir()与seekdir()
范例
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
main()
{
DIR * dir;
struct dirent * ptr;
int offset, offset_5, i = 0;
dir = opendir("/etc/rc.d"); // opendir
while((ptr = readdir(dir)) != NULL)
{
offset = telldir(dir);
if(++i == 5)
offset_5 = offset;
printf("d_name : %s offset :%d
", ptr->d_name, offset);
}
seekdir(dir offset_5);
printf("Readdir again!
");
while((ptr = readdir(dir)) != NULL)
{
offset = telldir(dir);
printf("d_name : %s offset :%d
", ptr->d_name, offset);
}
closedir(dir);
}
执行:
d_name : . offset :12
d_name : .. offset :24
d_name : init.d offset 40
d_name : rc0.d offset :56
d_name : rc1.d offset :72
d_name : rc2.d offset :88
d_name : rc3.d offset :104
d_name : rc4.d offset :120
d_name : rc5.d offset :136
d_name : rc6.d offset :152
d_name : rc offset :164
d_name : rc.local offset :180
d_name : rc.sysinit offset :4096 readdir again!
d_name : rc2.d offset :88
d_name : rc3.d offset :104
d_name : rc4.d offset :120
d_name : rc5.d offset :136
d_name : rc6.d offset :152
d_name : rc offset :164
d_name : rc.local offset :180
d_name : rc.sysinit offset :4096