1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8">
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>
14 <div class=
"para func">
16 <p>The f_open function opens a file.
</p>
19 FIL*
<span class=
"arg">fp
</span>,
<span class=
"c">/* [OUT] Pointer to the file object structure */
</span>
20 const TCHAR*
<span class=
"arg">path
</span>,
<span class=
"c">/* [IN] File name */
</span>
21 BYTE
<span class=
"arg">mode
</span> <span class=
"c">/* [IN] Mode flags */
</span>
26 <div class=
"para arg">
30 <dd>Pointer to the blank file object structure.
</dd>
32 <dd>Pointer to the null-terminated string that specifies the
<a href=
"filename.html">file name
</a> to open or create.
</dd>
34 <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>
36 <tr><th>Flags
</th><th>Meaning
</th></tr>
37 <tr><td>FA_READ
</td><td>Specifies read access to the file. Data can be read from the file.
</tr>
38 <tr><td>FA_WRITE
</td><td>Specifies write access to the file. Data can be written to the file. Combine with
<tt>FA_READ
</tt> for read-write access.
</td></tr>
39 <tr><td>FA_OPEN_EXISTING
</td><td>Opens a file. The function fails if the file is not existing. (Default)
</td></tr>
40 <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>
41 <tr><td>FA_CREATE_ALWAYS
</td><td>Creates a new file. If the file is existing, it will be truncated and overwritten.
</td></tr>
42 <tr><td>FA_OPEN_ALWAYS
</td><td>Opens the file if it is existing. If not, a new file will be created.
</td></tr>
43 <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>
45 Mode flags in POSIX fopen() function corresponds to FatFs mode flags as follows:
<br>
47 <tr><th>POSIX
</th><th>FatFs
</th></tr>
48 <tr><td>"r"</td><td>FA_READ
</td></tr>
49 <tr><td>"r+"</td><td>FA_READ | FA_WRITE
</td></tr>
50 <tr><td>"w"</td><td>FA_CREATE_ALWAYS | FA_WRITE
</td></tr>
51 <tr><td>"w+"</td><td>FA_CREATE_ALWAYS | FA_WRITE | FA_READ
</td></tr>
52 <tr><td>"a"</td><td>FA_OPEN_APPEND | FA_WRITE
</td></tr>
53 <tr><td>"a+"</td><td>FA_OPEN_APPEND | FA_WRITE | FA_READ
</td></tr>
54 <tr><td>"wx"</td><td>FA_CREATE_NEW | FA_WRITE
</td></tr>
55 <tr><td>"w+x"</td><td>FA_CREATE_NEW | FA_WRITE | FA_READ
</td></tr>
62 <div class=
"para ret">
63 <h4>Return Values
</h4>
65 <a href=
"rc.html#ok">FR_OK
</a>,
66 <a href=
"rc.html#de">FR_DISK_ERR
</a>,
67 <a href=
"rc.html#ie">FR_INT_ERR
</a>,
68 <a href=
"rc.html#nr">FR_NOT_READY
</a>,
69 <a href=
"rc.html#nf">FR_NO_FILE
</a>,
70 <a href=
"rc.html#np">FR_NO_PATH
</a>,
71 <a href=
"rc.html#in">FR_INVALID_NAME
</a>,
72 <a href=
"rc.html#dn">FR_DENIED
</a>,
73 <a href=
"rc.html#ex">FR_EXIST
</a>,
74 <a href=
"rc.html#io">FR_INVALID_OBJECT
</a>,
75 <a href=
"rc.html#wp">FR_WRITE_PROTECTED
</a>,
76 <a href=
"rc.html#id">FR_INVALID_DRIVE
</a>,
77 <a href=
"rc.html#ne">FR_NOT_ENABLED
</a>,
78 <a href=
"rc.html#ns">FR_NO_FILESYSTEM
</a>,
79 <a href=
"rc.html#tm">FR_TIMEOUT
</a>,
80 <a href=
"rc.html#lo">FR_LOCKED
</a>,
81 <a href=
"rc.html#nc">FR_NOT_ENOUGH_CORE
</a>,
82 <a href=
"rc.html#tf">FR_TOO_MANY_OPEN_FILES
</a>
87 <div class=
"para desc">
89 <p>The
<tt>f_open
</tt> function opens a file and creates a
<em>file object
</em>. The file object is an identifier for subsequent operations to 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 has been made and not closed prior to power off, media removal or re-mount, or the file can be collapsed.
</p>
90 <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>
94 <div class=
"para comp">
96 <p>Always available. Only
<tt>FA_READ
</tt> and
<tt>FA_OPEN_EXISTING
</tt> are available for the mode flags when
<tt><a href=
"config.html#fs_readonly">FF_FS_READONLY
</a> ==
1</tt>.
</p>
100 <div class=
"para use">
103 <span class=
"c">/* Read a text file and display it */
</span>
105 FATFS FatFs;
<span class=
"c">/* Work area (filesystem object) for logical drive */
</span>
109 FIL fil;
<span class=
"c">/* File object */
</span>
110 char line[
100];
<span class=
"c">/* Line buffer */
</span>
111 FRESULT fr;
<span class=
"c">/* FatFs return code */
</span>
114 <span class=
"c">/* Give a work area to the default drive */
</span>
115 f_mount(
&FatFs,
"",
0);
117 <span class=
"c">/* Open a text file */
</span>
118 fr =
<em>f_open
</em>(
&fil,
"message.txt", FA_READ);
119 if (fr) return (int)fr;
121 <span class=
"c">/* Read every line and display it */
</span>
122 while (f_gets(line, sizeof line,
&fil)) {
126 <span class=
"c">/* Close the file */
</span>
133 <span class=
"c">/* Copy a file
"file.bin" on the drive
1 to drive
0 */
</span>
137 FATFS fs0, fs1;
<span class=
"c">/* Work area (filesystem object) for logical drives */
</span>
138 FIL fsrc, fdst;
<span class=
"c">/* File objects */
</span>
139 BYTE buffer[
4096];
<span class=
"c">/* File copy buffer */
</span>
140 FRESULT fr;
<span class=
"c">/* FatFs function common result code */
</span>
141 UINT br, bw;
<span class=
"c">/* File read/write count */
</span>
144 <span class=
"c">/* Give work areas to each logical drive */
</span>
145 f_mount(
&fs0,
"0:",
0);
146 f_mount(
&fs1,
"1:",
0);
148 <span class=
"c">/* Open source file on the drive
1 */
</span>
149 fr =
<em>f_open
</em>(
&fsrc,
"1:file.bin", FA_READ);
150 if (fr) return (int)fr;
152 <span class=
"c">/* Create destination file on the drive
0 */
</span>
153 fr =
<em>f_open
</em>(
&fdst,
"0:file.bin", FA_WRITE | FA_CREATE_ALWAYS);
154 if (fr) return (int)fr;
156 <span class=
"c">/* Copy source to destination */
</span>
158 fr = f_read(
&fsrc, buffer, sizeof buffer,
&br);
<span class=
"c">/* Read a chunk of data from the source file */
</span>
159 if (br ==
0) break;
<span class=
"c">/* error or eof */
</span>
160 fr = f_write(
&fdst, buffer, br,
&bw);
<span class=
"c">/* Write it to the destination file */
</span>
161 if (bw
< br) break;
<span class=
"c">/* error or disk full */
</span>
164 <span class=
"c">/* Close open files */
</span>
168 <span class=
"c">/* Unregister work area prior to discard it */
</span>
178 <div class=
"para ref">
180 <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>
183 <p class=
"foot"><a href=
"../00index_e.html">Return
</a></p>