zoukankan      html  css  js  c++  java
  • dirent和DIR 结构体 表示文件夹中目录内容信息

    1. dirent  --- 用来表示某文件夹的目录内容。

    我猜是directory content 的缩写.

    dirent 定义于 /include/bits/dirent.h 中:

    1. struct dirent  
    2.   {  
    3. #ifndef __USE_FILE_OFFSET64   
    4.     __ino_t d_ino;            
    5.     __off_t d_off;  
    6. #else   
    7.     __ino64_t d_ino;  
    8.     __off64_t d_off;  
    9. #endif   
    10.     unsigned short int d_reclen;  
    11.     unsigned char d_type;  
    12.     char d_name[256];           /* We must not include limits.h! */  
    13.   };  
    14.                             
    15. #ifdef __USE_LARGEFILE64   
    16. struct dirent64  
    17.   {  
    18.     __ino64_t d_ino;  
    19.     __off64_t d_off;  
    20.     unsigned short int d_reclen;  
    21.     unsigned char d_type;  
    22.     char d_name[256];           /* We must not include limits.h! */  
    23.   };  
    24. #endif  
    可以看出64位的位宽和32位的位宽,定义有所区别,但是表示的内容还是一样的。

    d_ino  --- inode number 索引节点号.

    d_off --- offset to this dirent 在目录文件中的偏移.

    d_reclen --- length of this d_name 文件名长.

    d_type --- the type of d_name 文件类型.

    d_name[256] --- file name (null-terminated) 文件名

    2. DIR 结构体

    1. struct __dirstream     
    2.    {     
    3.     void *__fd;      
    4.     char *__data;      
    5.     int __entry_data;      
    6.     char *__ptr;      
    7.     int __entry_ptr;      
    8.     size_t __allocation;      
    9.     size_t __size;      
    10.     __libc_lock_define (, __lock)      
    11.    };     
    12.     
    13. typedef struct __dirstream DIR;    
    *__fd :'struct hurd_fd' pointer for descriptor.

    *__data :Directory block.

    __entry_data :Entry number `ponds to.
    *__ptr :Current pointer into the block.
    __allocation :Space allocated for the block.
    __size :Total valid data in the block.
    (, __lock) :Mutex lock for this structure.

    DIR结构体类似于FILE,是一个内部结构. 关于DIR结构体的内容,我们知道这么多就可以了,没必要去再去研究他的结构成员。

    以下几个函数用这个内部结构保存当前正在被读取的目录的有关信息.

    3.  几个 dirent,  DIR相关的常用函数 opendir(),readdir(),closedir() 等.

    函数 DIR *opendir(const char *pathname),即打开文件目录,返回的就是指向DIR结构体的指针,而该指针由以下几个函数使用:

    1. struct dirent *readdir(DIR *dp);     
    2.     
    3. void rewinddir(DIR *dp);     
    4.     
    5. int closedir(DIR *dp);     
    6.     
    7. long telldir(DIR *dp);     
    8.     
    9. void seekdir(DIR *dp,long loc);    
    4. 实例代码:列出某目录下所有文件夹功能。

    1. /* 
    2.  * This is a program to show opendir(), readdir(), closedir(). 
    3.  * The function of the program is to list the folders under the request dir 
    4.  */  
    5.   
    6. #include <stdio.h>   
    7. #include <errno.h>   
    8. #include <string.h>   
    9. #include <sys/types.h>   
    10. #include <dirent.h>   
    11.   
    12. #ifndef DT_DIR   
    13. #error "DT_DIR not defined, maybe d_type not a mumber of struct dirent!"   
    14. #endif   
    15.   
    16. int main(int argc, char*argv[])  
    17. {  
    18.     static char dot[] =".", dotdot[] ="..";  
    19.     const char *name;  
    20.     DIR *dirp;  
    21.     struct dirent *dp;  
    22.   
    23.     if (argc == 2)  
    24.         name = argv[1];  
    25.     else  
    26.         name = dot;  
    27.     printf(" the request dir name is %s\n", name);  
    28.   
    29.         //open the request dir.   
    30.     //return DIR pointer if open succeed.   
    31.     //return NULL if opendir failed.   
    32.     dirp = opendir(name);  
    33.     if (dirp == NULL) {  
    34.         fprintf(stderr, "%s: opendir(): %s: %s\n",  
    35.             argv[0], name, strerror(errno));  
    36.         exit(errno);  
    37.     } else {  
    38.         printf("opendir %s succeed!\n", name);    
    39.     }  
    40.   
    41.   
    42.     //readdir(dirent)   
    43.     //return dirent pointer if readdir succeed.   
    44.     //return NULL if readdir failed.   
    45.     while ((dp = readdir(dirp)) != NULL) {  
    46.         //判断文件类型是DT_DIR, 也就是目录   
    47.         if (dp->d_type == DT_DIR)  
    48.             //如果文件名不是"." 或者"..",就打印出来   
    49.             if ( strcmp(dp->d_name, dot)  
    50.                 && strcmp(dp->d_name, dotdot) )  
    51.                 printf("%s/\n", dp->d_name);  
    52.     }  
    53.   
    54.     //closedir(dirent)   
    55.     closedir(dirp);  
    56.     return (0);  
    57. }  

    5. 最后,总结一下,想要获取某目录下(比如a目下)b文件的详细信息,我们应该怎样做

    首先,我们使用opendir函数打开目录a,返回指向目录a的DIR结构体c。

    接着,我们调用readdir( c)函数读取目录a下所有文件(包括目录),返回指向目录a下所有文件的dirent结构体d。

    然后,我们遍历d,调用stat(d->name,stat *e)来获取每个文件的详细信息,存储在stat结构体e中。

    总体就是这样一种逐步细化的过程,在这一过程中,三种结构体扮演着不同的角色。

  • 相关阅读:
    PHP 开发 APP 接口 --Redis篇
    PHP 开发 APP 接口--静态缓存篇
    PHP 开发 APP 接口 --JSON、XML结合篇
    PHP 开发 APP 接口 --JSION篇
    PHP 开发 APP 接口--XML篇
    程序员必须掌握的600个英语单词
    Memcache
    伪静态
    ob缓冲
    函数的使用顺序---TABLES,USING,CHANGING
  • 原文地址:https://www.cnblogs.com/wangfengju/p/6173267.html
Copyright © 2011-2022 走看看