open.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">
  8. <title>FatFs - f_open</title>
  9. </head>
  10. <body>
  11. <div class="para">
  12. <h2>f_open</h2>
  13. <p>The f_open function creates a <em>file object</em> to be used to access the file.</p>
  14. <pre>
  15. FRESULT f_open (
  16. FIL* <em>FileObject</em>, /* Pointer to the blank file object structure */
  17. const char* <em>FileName</em>, /* Pointer to the file neme */
  18. BYTE <em>ModeFlags</em> /* Mode flags */
  19. );
  20. </pre>
  21. </div>
  22. <div class="para">
  23. <h4>Parameters</h4>
  24. <dl class="par">
  25. <dt>FileObject</dt>
  26. <dd>Pointer to the file object structure to be created. After the f_open funciton succeeded, the file can be accessed with the file object structure until it is closed.</dd>
  27. <dt>FileName</dt>
  28. <dd>Pointer to a null-terminated string that specifies the <a href="filename.html">file name</a> to create or open.</dd>
  29. <dt>ModeFlags</dt>
  30. <dd>Specifies the type of access and open method for the file. It is specified by a combination of following flags.<br>
  31. <table class="lst">
  32. <tr><th>Value</th><th>Description</th></tr>
  33. <tr><td>FA_READ</td><td>Specifies read access to the object. Data can be read from the file.<br>Combine with FA_WRITE for read-write access.</td></tr>
  34. <tr><td>FA_WRITE</td><td>Specifies write access to the object. Data can be written to the file.<br>Combine with FA_READ for read-write access.</td></tr>
  35. <tr><td>FA_OPEN_EXISTING</td><td>Opens the file. The function fails if the file is not existing. (Default)</td></tr>
  36. <tr><td>FA_OPEN_ALWAYS</td><td>Opens the file, if it is existing. If not, the function creates the new file.</td></tr>
  37. <tr><td>FA_CREATE_NEW</td><td>Creates a new file. The function fails if the file is already existing.</td></tr>
  38. <tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file is existing, it is truncated and overwritten.</td></tr>
  39. </table>
  40. </dd>
  41. </dl>
  42. </div>
  43. <div class="para">
  44. <h4>Return Values</h4>
  45. <dl class="ret">
  46. <dt>FR_OK (0)</dt>
  47. <dd>The function succeeded and the file object is valid.</dd>
  48. <dt>FR_NO_FILE</dt>
  49. <dd>Could not find the file.</dd>
  50. <dt>FR_NO_PATH</dt>
  51. <dd>Could not find the path.</dd>
  52. <dt>FR_INVALID_NAME</dt>
  53. <dd>The file name is invalid.</dd>
  54. <dt>FR_INVALID_DRIVE</dt>
  55. <dd>The drive number is invalid.</dd>
  56. <dt>FR_EXIST</dt>
  57. <dd>The file is already existing.</dd>
  58. <dt>FR_DENIED</dt>
  59. <dd>The required access was denied due to one of the following reasons:
  60. <ul><li>Write mode open of a read-only file.</li>
  61. <li>File cannot be created due to a read-only file or same name directory is existing.</li>
  62. <li>File cannot be created due to the directory table or disk is full.</li></ul></dd>
  63. <dt>FR_NOT_READY</dt>
  64. <dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd>
  65. <dt>FR_WRITE_PROTECTED</dt>
  66. <dd>Write mode open or creation under the medium is write protected.</dd>
  67. <dt>FR_RW_ERROR</dt>
  68. <dd>The function failed due to a disk error or an internal error.</dd>
  69. <dt>FR_NOT_ENABLED</dt>
  70. <dd>The logical drive has no work area.</dd>
  71. <dt>FR_NO_FILESYSTEM</dt>
  72. <dd>There is no valid FAT partition on the disk.</dd>
  73. </dl>
  74. </div>
  75. <div class="para">
  76. <h4>Description</h4>
  77. <p>The created file object is used for subsequent calls to refer to the file. When close an open file object, use <a href="close.html">f_close</a> function. If modified file is not closed correctly, the file will be collapsed.</p>
  78. <p>Before using any file function, a work area (file system object) must be given to the logical drive with <a href="mount.html">f_mount</a> function. All file functions can work after this procedure.</p>
  79. <p>The mode flags, <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW, FA_OPEN_ALWAYS</tt>, are not supported in read-only configuration.</p>
  80. </div>
  81. <div class="para">
  82. <h4>Example (File Copy)</h4>
  83. <pre>
  84. void main ()
  85. {
  86. FATFS fs; // Work area (file system object) for logical drive
  87. FIL fsrc, fdst; // file objects
  88. BYTE buffer[4096]; // file copy buffer
  89. FRESULT res; // FatFs function common result code
  90. UINT br, bw; // File R/W count
  91. // Register a work area for logical drive 0
  92. f_mount(0, &fs);
  93. // Open source file
  94. res = f_open(&fsrc, "srcfile.dat", FA_OPEN_EXISTING | FA_READ);
  95. if (res) die(res);
  96. // Create destination file
  97. res = f_open(&fdst, "dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
  98. if (res) die(res);
  99. // Copy source to destination
  100. for (;;) {
  101. res = f_read(&fsrc, buffer, sizeof(buffer), &br);
  102. if (res || br == 0) break; // error or eof
  103. res = f_write(&fdst, buffer, br, &bw);
  104. if (res || bw &lt; br) break; // error or disk full
  105. }
  106. // Close all files
  107. f_close(&fsrc);
  108. f_close(&fdst);
  109. // Unregister a work area before discard it
  110. f_mount(0, NULL);
  111. }
  112. </pre>
  113. </div>
  114. <div class="para">
  115. <h4>References</h4>
  116. <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>
  117. </div>
  118. <p class="foot"><a href="../00index_e.html">Return</a></p>
  119. </body>
  120. </html>