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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM