open.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/open.html">
  8. <link rel="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">
  9. <title>FatFs - f_open</title>
  10. </head>
  11. <body>
  12. <div class="para func">
  13. <h2>f_open</h2>
  14. <p>The f_open function opens a file.</p>
  15. <pre>
  16. FRESULT f_open (
  17. FIL* <span class="arg">fp</span>, <span class="c">/* [OUT] Pointer to the file object structure */</span>
  18. const TCHAR* <span class="arg">path</span>, <span class="c">/* [IN] File name */</span>
  19. BYTE <span class="arg">mode</span> <span class="c">/* [IN] Mode flags */</span>
  20. );
  21. </pre>
  22. </div>
  23. <div class="para arg">
  24. <h4>Parameters</h4>
  25. <dl class="par">
  26. <dt>fp</dt>
  27. <dd>Pointer to the blank file object structure.</dd>
  28. <dt>path</dt>
  29. <dd>Pointer to the null-terminated string that specifies the <a href="filename.html">file name</a> to open or create.</dd>
  30. <dt>mode</dt>
  31. <dd>Mode flags that specifies the type of access and open method for the file. It is specified by a combination of following flags.<br>
  32. <table class="lst">
  33. <tr><th>Flags</th><th>Meaning</th></tr>
  34. <tr><td>FA_READ</td><td>Specifies read access to the object. Data can be read from the file.</tr>
  35. <tr><td>FA_WRITE</td><td>Specifies write access to the object. Data can be written to the file. Combine with <tt>FA_READ</tt> for read-write access.</td></tr>
  36. <tr><td>FA_OPEN_EXISTING</td><td>Opens the file. The function fails if the file is not existing. (Default)</td></tr>
  37. <tr><td>FA_CREATE_NEW</td><td>Creates a new file. The function fails with <tt>FR_EXIST</tt> if the file is existing.</td></tr>
  38. <tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file is existing, it will be truncated and overwritten.</td></tr>
  39. <tr><td>FA_OPEN_ALWAYS</td><td>Opens the file if it is existing. If not, a new file will be created.</td></tr>
  40. <tr><td>FA_OPEN_APPEND</td><td>Same as <tt>FA_OPEN_ALWAYS</tt> except the read/write pointer is set end of the file.</td></tr>
  41. </table>
  42. Mode flags of POSIX fopen() corresponds to FatFs mode flags as follows:<br>
  43. <table class="lst">
  44. <tr><th>POSIX</th><th>FatFs</th></tr>
  45. <tr><td><tt>"r"</tt></td><td><tt>FA_READ</tt></td></tr>
  46. <tr><td><tt>"r+"</tt></td><td><tt>FA_READ | FA_WRITE</tt></td></tr>
  47. <tr><td><tt>"w"</tt></td><td><tt>FA_CREATE_ALWAYS | FA_WRITE</tt></td></tr>
  48. <tr><td><tt>"w+"</tt></td><td><tt>FA_CREATE_ALWAYS | FA_WRITE | FA_READ</tt></td></tr>
  49. <tr><td><tt>"a"</tt></td><td><tt>FA_OPEN_APPEND | FA_WRITE</tt></td></tr>
  50. <tr><td><tt>"a+"</tt></td><td><tt>FA_OPEN_APPEND | FA_WRITE | FA_READ</tt></td></tr>
  51. <tr><td><tt>"x"</tt><sup>*1</sup></td><td><tt>FA_CREATE_NEW | FA_WRITE</tt></td></tr>
  52. <tr><td><tt>"x+"</tt><sup>*1</sup></td><td><tt>FA_CREATE_NEW | FA_WRITE | FA_READ</tt></td></tr>
  53. </table>
  54. <sup>*1</sup>: glibc extension
  55. </dd>
  56. </dl>
  57. </div>
  58. <div class="para ret">
  59. <h4>Return Values</h4>
  60. <p>
  61. <a href="rc.html#ok">FR_OK</a>,
  62. <a href="rc.html#de">FR_DISK_ERR</a>,
  63. <a href="rc.html#ie">FR_INT_ERR</a>,
  64. <a href="rc.html#nr">FR_NOT_READY</a>,
  65. <a href="rc.html#nf">FR_NO_FILE</a>,
  66. <a href="rc.html#np">FR_NO_PATH</a>,
  67. <a href="rc.html#in">FR_INVALID_NAME</a>,
  68. <a href="rc.html#dn">FR_DENIED</a>,
  69. <a href="rc.html#ex">FR_EXIST</a>,
  70. <a href="rc.html#io">FR_INVALID_OBJECT</a>,
  71. <a href="rc.html#wp">FR_WRITE_PROTECTED</a>,
  72. <a href="rc.html#id">FR_INVALID_DRIVE</a>,
  73. <a href="rc.html#ne">FR_NOT_ENABLED</a>,
  74. <a href="rc.html#ns">FR_NO_FILESYSTEM</a>,
  75. <a href="rc.html#tm">FR_TIMEOUT</a>,
  76. <a href="rc.html#lo">FR_LOCKED</a>,
  77. <a href="rc.html#nc">FR_NOT_ENOUGH_CORE</a>,
  78. <a href="rc.html#tf">FR_TOO_MANY_OPEN_FILES</a>
  79. </p>
  80. </div>
  81. <div class="para desc">
  82. <h4>Description</h4>
  83. <p>The <tt>f_open</tt> function opens a file and creates a <em>file object</em>. The file object is used for subsequent read/write operations to the file to identify the file. Open file should be closed with <a href="close.html"><tt>f_close</tt></a> function after the session of the file access. If any change to the file is made and not closed prior to power down, media removal or re-mount, or the file can be collapsed.</p>
  84. <p>If duplicated file open is needed, read <a href="appnote.html#dup">here</a> carefully. However duplicated open of a file with any write mode flag is always prohibited.</p>
  85. </div>
  86. <div class="para comp">
  87. <h4>QuickInfo</h4>
  88. <p>Always available. Only <tt>FA_READ</tt> and <tt>FA_OPEN_EXISTING</tt> are supported when <tt>FF_FS_READONLY == 1</tt>.</p>
  89. </div>
  90. <div class="para use">
  91. <h4>Example</h4>
  92. <pre>
  93. <span class="c">/* Read a text file and display it */</span>
  94. FATFS FatFs; <span class="c">/* Work area (filesystem object) for logical drive */</span>
  95. int main (void)
  96. {
  97. FIL fil; <span class="c">/* File object */</span>
  98. char line[100]; <span class="c">/* Line buffer */</span>
  99. FRESULT fr; <span class="c">/* FatFs return code */</span>
  100. <span class="c">/* Register work area to the default drive */</span>
  101. f_mount(&amp;FatFs, "", 0);
  102. <span class="c">/* Open a text file */</span>
  103. fr = <em>f_open</em>(&amp;fil, "message.txt", FA_READ);
  104. if (fr) return (int)fr;
  105. <span class="c">/* Read all lines and display it */</span>
  106. while (f_gets(line, sizeof line, &amp;fil)) {
  107. printf(line);
  108. }
  109. <span class="c">/* Close the file */</span>
  110. f_close(&amp;fil);
  111. return 0;
  112. }
  113. </pre>
  114. <pre>
  115. <span class="c">/* Copy a file "file.bin" on the drive 1 to drive 0 */</span>
  116. int main (void)
  117. {
  118. FATFS fs[2]; <span class="c">/* Work area (filesystem object) for logical drives */</span>
  119. FIL fsrc, fdst; <span class="c">/* File objects */</span>
  120. BYTE buffer[4096]; <span class="c">/* File copy buffer */</span>
  121. FRESULT fr; <span class="c">/* FatFs function common result code */</span>
  122. UINT br, bw; <span class="c">/* File read/write count */</span>
  123. <span class="c">/* Register work area for each logical drive */</span>
  124. f_mount(&amp;fs[0], "0:", 0);
  125. f_mount(&amp;fs[1], "1:", 0);
  126. <span class="c">/* Open source file on the drive 1 */</span>
  127. fr = <em>f_open</em>(&amp;fsrc, "1:file.bin", FA_READ);
  128. if (fr) return (int)fr;
  129. <span class="c">/* Create destination file on the drive 0 */</span>
  130. fr = <em>f_open</em>(&amp;fdst, "0:file.bin", FA_WRITE | FA_CREATE_ALWAYS);
  131. if (fr) return (int)fr;
  132. <span class="c">/* Copy source to destination */</span>
  133. for (;;) {
  134. fr = f_read(&amp;fsrc, buffer, sizeof buffer, &amp;br); <span class="c">/* Read a chunk of source file */</span>
  135. if (fr || br == 0) break; <span class="c">/* error or eof */</span>
  136. fr = f_write(&amp;fdst, buffer, br, &amp;bw); <span class="c">/* Write it to the destination file */</span>
  137. if (fr || bw &lt; br) break; <span class="c">/* error or disk full */</span>
  138. }
  139. <span class="c">/* Close open files */</span>
  140. f_close(&amp;fsrc);
  141. f_close(&amp;fdst);
  142. <span class="c">/* Unregister work area prior to discard it */</span>
  143. f_mount(NULL, "0:", 0);
  144. f_mount(NULL, "1:", 0);
  145. return (int)fr;
  146. }
  147. </pre>
  148. </div>
  149. <div class="para ref">
  150. <h4>See Also</h4>
  151. <p><tt><a href="read.html">f_read</a>, <a href="write.html">f_write</a>, <a href="close.html">f_close</a>, <a href="sfile.html">FIL</a>, <a href="sfatfs.html">FATFS</a></tt></p>
  152. </div>
  153. <p class="foot"><a href="../00index_e.html">Return</a></p>
  154. </body>
  155. </html>