1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
\r
4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
\r
5 <meta http-equiv="Content-Style-Type" content="text/css">
\r
6 <link rel="up" title="FatFs" href="../00index_e.html">
\r
7 <link rel="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">
\r
8 <title>FatFs - f_open</title>
\r
15 <p>The f_open function creates a <em>file object</em> to be used to access the file.</p>
\r
18 FIL* <em>FileObject</em>, /* Pointer to the blank file object structure */
\r
19 const char* <em>FileName</em>, /* Pointer to the file neme */
\r
20 BYTE <em>ModeFlags</em> /* Mode flags */
\r
29 <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>
\r
31 <dd>Pointer to a null-terminated string that specifies the <a href="filename.html">file name</a> to create or open.</dd>
\r
33 <dd>Specifies the type of access and open method for the file. It is specified by a combination of following flags.<br>
\r
35 <tr><th>Value</th><th>Description</th></tr>
\r
36 <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>
\r
37 <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>
\r
38 <tr><td>FA_OPEN_EXISTING</td><td>Opens the file. The function fails if the file is not existing. (Default)</td></tr>
\r
39 <tr><td>FA_OPEN_ALWAYS</td><td>Opens the file, if it is existing. If not, the function creates the new file.</td></tr>
\r
40 <tr><td>FA_CREATE_NEW</td><td>Creates a new file. The function fails if the file is already existing.</td></tr>
\r
41 <tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file is existing, it is truncated and overwritten.</td></tr>
\r
49 <h4>Return Values</h4>
\r
52 <dd>The function succeeded and the file object is valid.</dd>
\r
54 <dd>Could not find the file.</dd>
\r
56 <dd>Could not find the path.</dd>
\r
57 <dt>FR_INVALID_NAME</dt>
\r
58 <dd>The file name is invalid.</dd>
\r
59 <dt>FR_INVALID_DRIVE</dt>
\r
60 <dd>The drive number is invalid.</dd>
\r
62 <dd>The file is already existing.</dd>
\r
64 <dd>The required access was denied due to one of the following reasons:
\r
65 <ul><li>Write mode open of a read-only file.</li>
\r
66 <li>File cannot be created due to a read-only file or same name directory is existing.</li>
\r
67 <li>File cannot be created due to the directory table or disk is full.</li></ul></dd>
\r
68 <dt>FR_NOT_READY</dt>
\r
69 <dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd>
\r
70 <dt>FR_WRITE_PROTECTED</dt>
\r
71 <dd>Write mode open or creation under the medium is write protected.</dd>
\r
72 <dt>FR_DISK_ERR</dt>
\r
73 <dd>The function failed due to an error in the disk function.</dd>
\r
75 <dd>The function failed due to a wrong FAT structure or an internal error.</dd>
\r
76 <dt>FR_NOT_ENABLED</dt>
\r
77 <dd>The logical drive has no work area.</dd>
\r
78 <dt>FR_NO_FILESYSTEM</dt>
\r
79 <dd>There is no valid FAT partition on the disk.</dd>
\r
85 <h4>Description</h4>
\r
86 <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, the file may be collapsed.</p>
\r
87 <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>
\r
88 <p>The mode flags, <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW, FA_OPEN_ALWAYS</tt>, are not available in read-only configuration.</p>
\r
93 <h4>Example (File Copy)</h4>
\r
97 FATFS fs; // Work area (file system object) for logical drive
\r
98 FIL fsrc, fdst; // file objects
\r
99 BYTE buffer[4096]; // file copy buffer
\r
100 FRESULT res; // FatFs function common result code
\r
101 UINT br, bw; // File R/W count
\r
104 // Register a work area for logical drive 0
\r
105 f_mount(0, &fs);
\r
107 // Open source file
\r
108 res = f_open(&fsrc, "srcfile.dat", FA_OPEN_EXISTING | FA_READ);
\r
111 // Create destination file
\r
112 res = f_open(&fdst, "dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
\r
115 // Copy source to destination
\r
117 res = f_read(&fsrc, buffer, sizeof(buffer), &br);
\r
118 if (res || br == 0) break; // error or eof
\r
119 res = f_write(&fdst, buffer, br, &bw);
\r
120 if (res || bw < br) break; // error or disk full
\r
124 f_close(&fsrc);
\r
125 f_close(&fdst);
\r
127 // Unregister a work area before discard it
\r
135 <h4>References</h4>
\r
136 <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>
\r
139 <p class="foot"><a href="../00index_e.html">Return</a></p>
\r