FATFS 初学之 f_opendir/ f_readdir

f_opendir:

 1 /*-----------------------------------------------------------------------*/
 2 /* Create a Directroy Object                                             */
 3 /*-----------------------------------------------------------------------*/
 4 
 5 FRESULT f_opendir (
 6     DIR *dj,            /* Pointer to directory object to create */
 7     const TCHAR *path    /* Pointer to the directory path */
 8 )
 9 {
10     FRESULT res;
11     DEF_NAMEBUF;
12 
13 
14     res = chk_mounted(&path, &dj->fs, 0);
15     if (res == FR_OK) {
16         INIT_BUF(*dj);
17         res = follow_path(dj, path);            /* Follow the path to the directory */
18         FREE_BUF();
19         if (res == FR_OK) {                        /* Follow completed */
20             if (dj->dir) {                        /* It is not the root dir */
21                 if (dj->dir[DIR_Attr] & AM_DIR) {    /* The object is a directory */
22                     dj->sclust = LD_CLUST(dj->dir);
23                 } else {                        /* The object is not a directory */
24                     res = FR_NO_PATH;
25                 }
26             }
27             if (res == FR_OK) {
28                 dj->id = dj->fs->id;
29                 res = dir_sdi(dj, 0);            /* Rewind dir */
30             }
31         }
32         if (res == FR_NO_FILE) res = FR_NO_PATH;
33     }
34 
35     LEAVE_FF(dj->fs, res);
36 }
View Code

函数功能:打开一个目录

描述:

f_opendir函数当_FS_MINIMIZE <= 1时可用。
f_opendir函数打开一个已存在的目录,并为后续的调用创建一个目录对象。该目录对象结构可以在任何时候不经任何步骤而被丢弃。

f_readdir:

 1 /*-----------------------------------------------------------------------*/
 2 /* Read Directory Entry in Sequense                                      */
 3 /*-----------------------------------------------------------------------*/
 4 
 5 FRESULT f_readdir (
 6     DIR *dj,            /* Pointer to the open directory object */
 7     FILINFO *fno        /* Pointer to file information to return */
 8 )
 9 {
10     FRESULT res;
11     DEF_NAMEBUF;
12 
13 
14     res = validate(dj->fs, dj->id);            /* Check validity of the object */
15     if (res == FR_OK) {
16         if (!fno) {
17             res = dir_sdi(dj, 0);            /* Rewind the directory object */
18         } else {
19             INIT_BUF(*dj);
20             res = dir_read(dj);                /* Read an directory item */
21             if (res == FR_NO_FILE) {        /* Reached end of dir */
22                 dj->sect = 0;
23                 res = FR_OK;
24             }
25             if (res == FR_OK) {                /* A valid entry is found */
26                 get_fileinfo(dj, fno);        /* Get the object information */
27                 res = dir_next(dj, 0);        /* Increment index for next */
28                 if (res == FR_NO_FILE) {
29                     dj->sect = 0;
30                     res = FR_OK;
31                 }
32             }
33             FREE_BUF();
34         }
35     }
36 
37     LEAVE_FF(dj->fs, res);
38 }
View Code

函数功能:读取目录项

描述:

f_readdir函数当 _FS_MINIMIZE <= 1时可用。
f_readdir函数顺序读取目录项。目录中的所有项可以通过重复调用f_readdir函数被读取。当所有目录项已被读取并且没有项要读取时,该函数没有任何错误地返回一个空字符串到f_name[]成员中。当 FileInfo给定一个空指针时,目录对象的读索引将被回绕。
当LFN功能被使能时,在使用f_readdir函数之前,文件信息结构中的lfname和lfsize必须被初始化为有效数值。lfname是一个返回长文件名的字符串缓冲区指针。lfsize是以字符为单位的字符串缓冲区的大小。如果读缓冲区或LFN工作缓冲区的大小(对于LFN)不足,或者对象没有LFN,则一个空字符串将被返回到LFN读缓冲区。

如果LFN包含任何不能被转换为 OEM代码的字符,则一个空字符串将被返回,但是这不是 Unicode API配置的情况。当 lfname是一个空字符串时,没有LFN的任何数据被返回。当对象没有 LFN时,任何小型大写字母可以被包含在SFN中。
当相对路径功能被使能(_FS_RPATH == 1)时,"."和".."目录项不会被过滤掉,并且它将出现在读目录项中。

例:

 1 DIR dir;
 2 FILINFO f_info;
 3 #define MAXDIR 80
 4 
 5 // 以下代码实现读取根目录下所有的子目录名(短文件名)并保存到数组Dirname[MAXDIR][13]中,仅供参考.
 6 if(f_opendir(&dir,"")!=FR_OK)
 7 {
 8     // 错误处理代码...
 9 }
10 memset(Dirname[0],0,13*MAXDIR);
11 maxdir=1;
12 
13 while(1)
14 {
15     if(f_readdir(&dir,&f_info)==FR_OK)
16     {
17         if(f_info.fname[0]==0)break;
18     }
19     else break;
20     
21     if(f_info.fattrib & AM_DIR)        // 目录
22     {
23         if(maxdir<MAXDIR)
24         {
25             strncpy(Dirname[maxdir],f_info.fname,13);
26             maxdir++;
27         }
28     }
29 }
View Code
原文地址:https://www.cnblogs.com/Danhuise/p/3910288.html