dir

struct dirent 与 DIR

1.存储目录中的文件信息(文件名、扩展名等等)

#include <dirent.h>
struct dirent
{
   long d_ino; /* inode number 索引节点号 */
   off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
   unsigned short d_reclen; /* length of this d_name 文件名长 */
   unsigned char d_type; /* the type of d_name 文件类型 */
   char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
}
 
2.
struct __dirstream
   {
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 \n", ptr->d_name, offset);
    }
    seekdir(dir offset_5);
    printf("Readdir again!\n");

    while((ptr = readdir(dir)) != NULL)
    {
        offset = telldir(dir);
        printf("d_name : %s offset :%d\n", 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

posted on 2015-06-23 17:15  DayAfterDay  阅读(227)  评论(0编辑  收藏  举报

导航