open.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 creates a <em>file object</em> to be used to access the file.</p>
  15. <pre>
  16. FRESULT f_open (
  17. FIL* <em>FileObject</em>, <span class="c">/* Pointer to the blank file object structure */</span>
  18. const TCHAR* <em>FileName</em>, <span class="c">/* Pointer to the file neme */</span>
  19. BYTE <em>ModeFlags</em> <span class="c">/* Mode flags */</span>
  20. );
  21. </pre>
  22. </div>
  23. <div class="para arg">
  24. <h4>Parameters</h4>
  25. <dl class="par">
  26. <dt>FileObject</dt>
  27. <dd>Pointer to the file object structure to be created.</dd>
  28. <dt>FileName</dt>
  29. <dd>Pointer to a null-terminated string that specifies the <a href="filename.html">file name</a> to create or open.</dd>
  30. <dt>ModeFlags</dt>
  31. <dd>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>Value</th><th>Description</th></tr>
  34. <tr><td>FA_READ</td><td>Specifies read access to the object. Data can be read from the file.<br>Combine with <tt>FA_WRITE</tt> for read-write access.</td></tr>
  35. <tr><td>FA_WRITE</td><td>Specifies write access to the object. Data can be written to the file.<br>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_OPEN_ALWAYS</td><td>Opens the file if it is existing. If not, a new file is created.<br>
  38. To append data to the file, use <a href="lseek.html">f_lseek</a> function after file open in this method.</td></tr>
  39. <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>
  40. <tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file is existing, it is truncated and overwritten.</td></tr>
  41. </table>
  42. </dd>
  43. </dl>
  44. </div>
  45. <div class="para ret">
  46. <h4>Return Values</h4>
  47. <p>
  48. <a href="rc.html#ok">FR_OK</a>,
  49. <a href="rc.html#de">FR_DISK_ERR</a>,
  50. <a href="rc.html#ie">FR_INT_ERR</a>,
  51. <a href="rc.html#nr">FR_NOT_READY</a>,
  52. <a href="rc.html#ok">FR_NO_FILE</a>,
  53. <a href="rc.html#np">FR_NO_PATH</a>,
  54. <a href="rc.html#in">FR_INVALID_NAME</a>,
  55. <a href="rc.html#de">FR_DENIED</a>,
  56. <a href="rc.html#ex">FR_EXIST</a>,
  57. <a href="rc.html#wp">FR_WRITE_PROTECTED</a>,
  58. <a href="rc.html#id">FR_INVALID_DRIVE</a>,
  59. <a href="rc.html#ne">FR_NOT_ENABLED</a>,
  60. <a href="rc.html#ns">FR_NO_FILESYSTEM</a>,
  61. <a href="rc.html#tm">FR_TIMEOUT</a>,
  62. <a href="rc.html#lo">FR_LOCKED</a>,
  63. <a href="rc.html#nc">FR_NOT_ENOUGH_CORE</a>,
  64. <a href="rc.html#tf">FR_TOO_MANY_OPEN_FILES</a>
  65. </p>
  66. </div>
  67. <div class="para desc">
  68. <h4>Description</h4>
  69. <p>After f_open function succeeded, the file object is valid. The file object is used for subsequent read/write functions to identify the file. When close an open file object, use <a href="close.html">f_close</a> function. If the modified file is not closed, the file data can be collapsed.</p>
  70. <p>If duplicated file open is needed, read <a href="appnote.html#dup">here</a> carefully.</p>
  71. <p>Before using any file function, a work area (file system object) must be registered to the logical drive with <a href="mount.html">f_mount</a> function. All API functions except for <a href="fdisk.html">f_fdisk</a> function can work after this procedure.</p>
  72. </div>
  73. <div class="para comp">
  74. <h4>QuickInfo</h4>
  75. <p>Always available. The mode flags, <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW and FA_OPEN_ALWAYS</tt>, are not available when <tt>_FS_READONLY == 1</tt>.</p>
  76. </div>
  77. <div class="para use">
  78. <h4>Example (File Copy)</h4>
  79. <pre>
  80. void main (void)
  81. {
  82. FATFS fs[2]; <span class="c">/* Work area (file system object) for logical drives */</span>
  83. FIL fsrc, fdst; <span class="c">/* file objects */</span>
  84. BYTE buffer[4096]; <span class="c">/* file copy buffer */</span>
  85. FRESULT res; <span class="c">/* FatFs function common result code */</span>
  86. UINT br, bw; <span class="c">/* File read/write count */</span>
  87. <span class="c">/* Register work area for each volume (Always succeeds regardless of disk status) */</span>
  88. f_mount(0, &amp;fs[0]);
  89. f_mount(1, &amp;fs[1]);
  90. <span class="c">/* Open source file on the drive 1 */</span>
  91. res = f_open(&amp;fsrc, "1:srcfile.dat", FA_OPEN_EXISTING | FA_READ);
  92. if (res) die(res);
  93. <span class="c">/* Create destination file on the drive 0 */</span>
  94. res = f_open(&amp;fdst, "0:dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
  95. if (res) die(res);
  96. <span class="c">/* Copy source to destination */</span>
  97. for (;;) {
  98. res = f_read(&amp;fsrc, buffer, sizeof buffer, &amp;br); <span class="c">/* Read a chunk of src file */</span>
  99. if (res || br == 0) break; <span class="c">/* error or eof */</span>
  100. res = f_write(&amp;fdst, buffer, br, &amp;bw); <span class="c">/* Write it to the dst file */</span>
  101. if (res || bw &lt; br) break; <span class="c">/* error or disk full */</span>
  102. }
  103. <span class="c">/* Close open files */</span>
  104. f_close(&amp;fsrc);
  105. f_close(&amp;fdst);
  106. <span class="c">/* Unregister work area prior to discard it */</span>
  107. f_mount(0, NULL);
  108. f_mount(1, NULL);
  109. }
  110. </pre>
  111. </div>
  112. <div class="para ref">
  113. <h4>See Also</h4>
  114. <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>
  115. </div>
  116. <p class="foot"><a href="../00index_e.html">Return</a></p>
  117. </body>
  118. </html>