readdir.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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=iso-8859-1">
  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. <body>
  12. <div class="para func">
  13. <h2>f_readdir</h2>
  14. <p>The f_readdir function reads directory entries.</p>
  15. <pre>
  16. FRESULT f_readdir (
  17. DIR* <span class="arg">dp</span>, <span class="c">/* [IN] Directory object */</span>
  18. FILINFO* <span class="arg">fno</span> <span class="c">/* [OUT] File information structure */</span>
  19. );
  20. </pre>
  21. </div>
  22. <div class="para arg">
  23. <h4>Parameters</h4>
  24. <dl class="par">
  25. <dt>dp</dt>
  26. <dd>Pointer to the open directory object.</dd>
  27. <dt>fno</dt>
  28. <dd>Pointer to the file information structure to store the read item.</dd>
  29. </dl>
  30. </div>
  31. <div class="para ret">
  32. <h4>Return Values</h4>
  33. <p>
  34. <a href="rc.html#ok">FR_OK</a>,
  35. <a href="rc.html#de">FR_DISK_ERR</a>,
  36. <a href="rc.html#ie">FR_INT_ERR</a>,
  37. <a href="rc.html#nr">FR_NOT_READY</a>,
  38. <a href="rc.html#io">FR_INVALID_OBJECT</a>,
  39. <a href="rc.html#tm">FR_TIMEOUT</a>,
  40. <a href="rc.html#nc">FR_NOT_ENOUGH_CORE</a>
  41. </p>
  42. </div>
  43. <div class="para desc">
  44. <h4>Description</h4>
  45. <p>The <tt>f_readdir()</tt> function reads directory items, file and directory, in sequence. All items in the directory can be read by calling <tt>f_readdir()</tt> function repeatedly. When relative path feature is enabled (<tt>_FS_RPATH &gt;= 1</tt>), dot entries ("." and "..") are not filtered out and they will appear in the read items. When all directory items have been read and no item to read, a null string is returned into the <tt>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.</p>
  46. <p>When LFN feature is enabled, <tt>lfname</tt> and <tt>lfsize</tt> in the file information structure must be initialized with valid value prior to use it. The <tt>lfname</tt> is a pointer to the LFN read buffer. The <tt>lfsize</tt> is size of the LFN read buffer in unit of <tt>TCHAR</tt>. If the LFN is not needed, set a null pointer to the <tt>lfname</tt> and the LFN is not returned. A null string will be returned into the LFN read buffer in case of following conditions.</p>
  47. <ul>
  48. <li>The directory item has no LFN information.</li>
  49. <li>Either the size of read buffer or LFN working buffer is insufficient for the LFN.</li>
  50. <li>The LFN contains any Unicode character that cannot be converted to OEM code. (not the case at Unicode API cfg.)</li>
  51. </ul>
  52. <p>When the directory item has no LFN information, lower case characters can be contained in the <tt>fname[]</tt>.</p>
  53. </div>
  54. <div class="para comp">
  55. <h4>QuickInfo</h4>
  56. <p>Available when <tt>_FS_MINIMIZE &lt;= 1</tt>.</p>
  57. </div>
  58. <div class="para use">
  59. <h4>Sample Code</h4>
  60. <pre>
  61. FRESULT scan_files (
  62. char* path <span class="c">/* Start node to be scanned (also used as work area) */</span>
  63. )
  64. {
  65. FRESULT res;
  66. FILINFO fno;
  67. DIR dir;
  68. int i;
  69. char *fn; <span class="c">/* This function is assuming non-Unicode cfg. */</span>
  70. <span class="k">#if</span> _USE_LFN
  71. static char lfn[_MAX_LFN + 1]; <span class="c">/* Buffer to store the LFN */</span>
  72. fno.lfname = lfn;
  73. fno.lfsize = sizeof lfn;
  74. <span class="k">#endif</span>
  75. res = f_opendir(&amp;dir, path); <span class="c">/* Open the directory */</span>
  76. if (res == FR_OK) {
  77. i = strlen(path);
  78. for (;;) {
  79. res = f_readdir(&amp;dir, &amp;fno); <span class="c">/* Read a directory item */</span>
  80. if (res != FR_OK || fno.fname[0] == 0) break; <span class="c">/* Break on error or end of dir */</span>
  81. if (fno.fname[0] == '.') continue; <span class="c">/* Ignore dot entry */</span>
  82. <span class="k">#if</span> _USE_LFN
  83. fn = *fno.lfname ? fno.lfname : fno.fname;
  84. <span class="k">#else</span>
  85. fn = fno.fname;
  86. <span class="k">#endif</span>
  87. if (fno.fattrib &amp; AM_DIR) { <span class="c">/* It is a directory */</span>
  88. sprintf(&amp;path[i], "/%s", fn);
  89. res = scan_files(path);
  90. if (res != FR_OK) break;
  91. path[i] = 0;
  92. } else { <span class="c">/* It is a file. */</span>
  93. printf("%s/%s\n", path, fn);
  94. }
  95. }
  96. f_closedir(&amp;dir)
  97. }
  98. return res;
  99. }
  100. </pre>
  101. </div>
  102. <div class="para ref">
  103. <h4>See Also</h4>
  104. <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>
  105. </div>
  106. <p class="foot"><a href="../00index_e.html">Return</a></p>
  107. </body>
  108. </html>