• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2<html lang="en">
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5<meta http-equiv="Content-Style-Type" content="text/css">
6<link rel="up" title="FatFs" href="../00index_e.html">
7<link rel="alternate" hreflang="ja" title="Japanese" href="../ja/readdir.html">
8<link rel="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">
9<title>FatFs - f_readdir</title>
10</head>
11
12<body>
13
14<div class="para func">
15<h2>f_readdir</h2>
16<p>The f_readdir function reads an item of the directory.</p>
17<pre>
18FRESULT f_readdir (
19  DIR* <span class="arg">dp</span>,      <span class="c">/* [IN] Directory object */</span>
20  FILINFO* <span class="arg">fno</span>  <span class="c">/* [OUT] File information structure */</span>
21);
22</pre>
23<pre>
24FRESULT f_rewinddir (
25  DIR* <span class="arg">dp</span>       <span class="c">/* [IN] Directory object */</span>
26);
27</pre>
28</div>
29
30<div class="para arg">
31<h4>Parameters</h4>
32<dl class="par">
33<dt>dp</dt>
34<dd>Pointer to the open directory object.</dd>
35<dt>fno</dt>
36<dd>Pointer to the <a href="sfileinfo.html">file information structure</a> to store the information about read item. A null pointer rewinds the read index of the directory.</dd>
37</dl>
38</div>
39
40
41<div class="para ret">
42<h4>Return Values</h4>
43<p>
44<a href="rc.html#ok">FR_OK</a>,
45<a href="rc.html#de">FR_DISK_ERR</a>,
46<a href="rc.html#ie">FR_INT_ERR</a>,
47<a href="rc.html#io">FR_INVALID_OBJECT</a>,
48<a href="rc.html#tm">FR_TIMEOUT</a>,
49<a href="rc.html#nc">FR_NOT_ENOUGH_CORE</a>
50</p>
51</div>
52
53
54<div class="para desc">
55<h4>Description</h4>
56<p>The <tt>f_readdir</tt> function reads a directory item, informations about the object, from the open directory. Items in the directory can be read in sequence by <tt>f_readdir</tt> function calls. When all items in the directory have been read and no item to read, a null string is stored into the <tt>fno-&gt;fname[]</tt> without any error. When a null pointer is given to the <tt class="arg">fno</tt>, the read index of the directory object is rewinded. The <tt>f_rewinddir</tt> function is implemented as a macro.</p>
57<pre>
58#define <em>f_rewinddir</em>(dp) f_readdir((dp), 0)
59</pre>
60<p>When LFN is enabled, a member <tt>altname[]</tt> is defined in the file information structure to store the short file name of the object. If the long file name is not accessible due to a reason listed below, short file name is stored to the <tt>fname[]</tt> and the <tt>altname[]</tt> has a null string.</p>
61<ul>
62<li>The item has no LFN. (Not the case in exFAT volume)</li>
63<li><a href="config.html#max_lfn"><tt>FF_MAX_LFN</tt></a> is insufficient to handle the LFN. (Not the case in <tt>FF_MAX_LFN == 255</tt>)</li>
64<li><a href="config.html#lfn_buf"><tt>FF_LFN_BUF</tt></a> is insufficient to store the LFN.</li>
65<li>The LFN contains some character not defined in current CP. (Not the case in <tt>FF_LFN_UNICODE != 0</tt>)</li>
66</ul>
67<p>There is an issue on read directories in exFAT volume. The exFAT does not support short file name. This means no name can be returned on the condition above. If it is the case, "?" is returned as the file name to indicate that the object is not accessible. To avoid this problem, configure FatFs <tt><a href="config.html#lfn_unicode">FF_LFN_UNICODE</a> != 0</tt> and <tt>FF_MAX_LFN == 255</tt> to support the full feature of LFN specification.</p>
68<p>Dot entries (<tt>"."</tt> and <tt>".."</tt>) in the sub-directory of FAT volume are filtered out and they will never appear in the read items because exFAT lacks dot entries in the sub-directory.</p>
69</div>
70
71
72<div class="para comp">
73<h4>QuickInfo</h4>
74<p>Available when <tt><a href="config.html#fs_minimize">FF_FS_MINIMIZE</a> &lt;= 1</tt>.</p>
75</div>
76
77
78<div class="para use">
79<h4>Sample Code</h4>
80<pre>
81FRESULT scan_files (
82    char* path        <span class="c">/* Start node to be scanned (***also used as work area***) */</span>
83)
84{
85    FRESULT res;
86    DIR dir;
87    UINT i;
88    static FILINFO fno;
89
90
91    res = f_opendir(&amp;dir, path);                       <span class="c">/* Open the directory */</span>
92    if (res == FR_OK) {
93        for (;;) {
94            res = <em>f_readdir</em>(&amp;dir, &amp;fno);                   <span class="c">/* Read a directory item */</span>
95            if (res != FR_OK || fno.fname[0] == 0) break;  <span class="c">/* Break on error or end of dir */</span>
96            if (fno.fattrib &amp; AM_DIR) {                    <span class="c">/* It is a directory */</span>
97                i = strlen(path);
98                sprintf(&amp;path[i], "/%s", fno.fname);
99                res = scan_files(path);                    <span class="c">/* Enter the directory */</span>
100                if (res != FR_OK) break;
101                path[i] = 0;
102            } else {                                       <span class="c">/* It is a file. */</span>
103                printf("%s/%s\n", path, fno.fname);
104            }
105        }
106        f_closedir(&amp;dir);
107    }
108
109    return res;
110}
111
112
113int main (void)
114{
115    FATFS fs;
116    FRESULT res;
117    char buff[256];
118
119
120    res = f_mount(&amp;fs, "", 1);
121    if (res == FR_OK) {
122        strcpy(buff, "/");
123        res = scan_files(buff);
124    }
125
126    return res;
127}
128</pre>
129</div>
130
131
132<div class="para ref">
133<h4>See Also</h4>
134<p><tt><a href="opendir.html">f_opendir</a>, <a href="closedir.html">f_closedir</a>, <a href="stat.html">f_stat</a>, <a href="sfileinfo.html">FILINFO</a>, <a href="sdir.html">DIR</a></tt></p>
135</div>
136
137<p class="foot"><a href="../00index_e.html">Return</a></p>
138</body>
139</html>
140