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中。

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤来实现获取多个日期文件夹内的文件数量和名字,并且通过结构体保存下来: 1. 定义一个结构体来保存文件名和数量: ``` typedef struct { char filename[100]; int count; } FileInfo; ``` 2. 编写一个函数来获取文件夹内的文件数量和名字: ``` int get_file_info(char *folder_path, FileInfo *file_info) { int file_count = 0; DIR *dir; struct dirent *entry; dir = opendir(folder_path); if (dir == NULL) { perror("opendir error"); return -1; } while ((entry = readdir(dir)) != NULL) { if (entry->d_type == DT_REG) { strcpy(file_info[file_count].filename, entry->d_name); file_info[file_count].count = 0; file_count++; } } closedir(dir); return file_count; } ``` 这个函数接受一个文件夹路径和一个保存文件信息结构体数组,返回文件数量。它会遍历文件夹内的所有文件,如果是普通文件,就将文件名保存到结构体,并将数量初始化为0。 3. 编写一个函数来获取每个文件的数量: ``` void count_file(char *folder_path, FileInfo *file_info, int file_count) { DIR *dir; struct dirent *entry; struct stat statbuf; char path[256]; for (int i = 0; i < file_count; i++) { int count = 0; sprintf(path, "%s/%s", folder_path, file_info[i].filename); dir = opendir(path); if (dir == NULL) { perror("opendir error"); return; } while ((entry = readdir(dir)) != NULL) { if (entry->d_type == DT_REG) { char file_path[256]; sprintf(file_path, "%s/%s", path, entry->d_name); if (stat(file_path, &statbuf) == 0) { count++; } } } closedir(dir); file_info[i].count = count; } } ``` 这个函数接受文件夹路径、文件信息结构体数组和文件数量作为参数。它会遍历每个文件夹,统计文件数量,并将数量保存到对应的结构体。 4. 主函数调用以上两个函数: ``` int main() { char folder_path[256] = "/path/to/folder"; FileInfo file_info[100]; int file_count = get_file_info(folder_path, file_info); count_file(folder_path, file_info, file_count); for (int i = 0; i < file_count; i++) { printf("filename: %s, count: %d\n", file_info[i].filename, file_info[i].count); } return 0; } ``` 这个示例代码的`folder_path`可以替换成你要处理的日期文件夹的路径。运行程序后,它会输出每个文件的名字和数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值