• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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=utf-8">
5<meta http-equiv="Content-Style-Type" content="text/css">
6<link rel="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">
7<title>FatFs - f_open</title>
8</head>
9
10<body>
11
12<div class="para func">
13<h2>f_open</h2>
14<p>The f_open function opens a file.</p>
15<pre>
16FRESULT 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
24<div class="para arg">
25<h4>Parameters</h4>
26<dl class="par">
27<dt>fp</dt>
28<dd>Pointer to the blank file object structure. If a null pointer is given, the function fails with <tt>FR_INVALID_OBJECT</tt>.</dd>
29<dt>path</dt>
30<dd>Pointer to the null-terminated string that specifies the <a href="filename.html">file name</a> to open or create. If a null pointer is given, the function fails with <tt>FR_INVALID_DRIVE</tt>.</dd>
31<dt>mode</dt>
32<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>
33<table class="lst">
34<tr><th>Flags</th><th>Meaning</th></tr>
35<tr><td>FA_READ</td><td>Specifies read access to the file. Data can be read from the file.</tr>
36<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>
37<tr><td>FA_OPEN_EXISTING</td><td>Opens the file. The function fails if the file is not existing. (Default)</td></tr>
38<tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file is existing, the file is truncated and overwritten.</td></tr>
39<tr><td>FA_CREATE_NEW</td><td>Creates a new file. The function fails if the file is existing.</td></tr>
40<tr><td>FA_OPEN_ALWAYS</td><td>Opens the file. If it is not exist, a new file is created.</td></tr>
41<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>
42</table>
43Mode flags in POSIX fopen() function corresponds to FatFs mode flags as follows:<br>
44<table class="lst2">
45<tr><th>POSIX</th><th>FatFs</th></tr>
46<tr><td>"r"</td><td>FA_READ</td></tr>
47<tr><td>"r+"</td><td>FA_READ | FA_WRITE</td></tr>
48<tr><td>"w"</td><td>FA_CREATE_ALWAYS | FA_WRITE</td></tr>
49<tr><td>"w+"</td><td>FA_CREATE_ALWAYS | FA_WRITE | FA_READ</td></tr>
50<tr><td>"a"</td><td>FA_OPEN_APPEND | FA_WRITE</td></tr>
51<tr><td>"a+"</td><td>FA_OPEN_APPEND | FA_WRITE | FA_READ</td></tr>
52<tr><td>"wx"</td><td>FA_CREATE_NEW | FA_WRITE</td></tr>
53<tr><td>"w+x"</td><td>FA_CREATE_NEW | FA_WRITE | FA_READ</td></tr>
54</table>
55</dd>
56</dl>
57</div>
58
59
60<div class="para ret">
61<h4>Return Values</h4>
62<p>
63<a href="rc.html#ok">FR_OK</a>,
64<a href="rc.html#de">FR_DISK_ERR</a>,
65<a href="rc.html#ie">FR_INT_ERR</a>,
66<a href="rc.html#nr">FR_NOT_READY</a>,
67<a href="rc.html#nf">FR_NO_FILE</a>,
68<a href="rc.html#np">FR_NO_PATH</a>,
69<a href="rc.html#in">FR_INVALID_NAME</a>,
70<a href="rc.html#dn">FR_DENIED</a>,
71<a href="rc.html#ex">FR_EXIST</a>,
72<a href="rc.html#io">FR_INVALID_OBJECT</a>,
73<a href="rc.html#wp">FR_WRITE_PROTECTED</a>,
74<a href="rc.html#id">FR_INVALID_DRIVE</a>,
75<a href="rc.html#ne">FR_NOT_ENABLED</a>,
76<a href="rc.html#ns">FR_NO_FILESYSTEM</a>,
77<a href="rc.html#tm">FR_TIMEOUT</a>,
78<a href="rc.html#lo">FR_LOCKED</a>,
79<a href="rc.html#nc">FR_NOT_ENOUGH_CORE</a>,
80<a href="rc.html#tf">FR_TOO_MANY_OPEN_FILES</a>
81</p>
82</div>
83
84
85<div class="para desc">
86<h4>Description</h4>
87<p>The <tt>f_open</tt> function opens a file and creates a <em>file object</em>. It is the identifier for subsequent read/write operations to the file. After the function succeeded, the file object is valid. If the function failed, the file object is set invalid.</p>
88<p>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>
89<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>
90</div>
91
92
93<div class="para comp">
94<h4>QuickInfo</h4>
95<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>
96</div>
97
98
99<div class="para use">
100<h4>Example</h4>
101<pre>
102<span class="c">/* Read a text file and display it */</span>
103
104FATFS FatFs;   <span class="c">/* Work area (filesystem object) for logical drive */</span>
105
106int main (void)
107{
108    FIL fil;        <span class="c">/* File object */</span>
109    char line[100]; <span class="c">/* Line buffer */</span>
110    FRESULT fr;     <span class="c">/* FatFs return code */</span>
111
112
113    <span class="c">/* Give a work area to the default drive */</span>
114    <em>f_mount</em>(&amp;FatFs, "", 0);
115
116    <span class="c">/* Open a text file */</span>
117    fr = <em>f_open</em>(&amp;fil, "message.txt", FA_READ);
118    if (fr) return (int)fr;
119
120    <span class="c">/* Read every line and display it */</span>
121    while (<em>f_gets</em>(line, sizeof line, &amp;fil)) {
122        printf(line);
123    }
124
125    <span class="c">/* Close the file */</span>
126    <em>f_close</em>(&amp;fil);
127
128    return 0;
129}
130</pre>
131<pre>
132<span class="c">/* Copy a file "file.bin" on the drive 1 to drive 0 */</span>
133
134int main (void)
135{
136    FATFS fs0, fs1;      <span class="c">/* Work area (filesystem object) for logical drives */</span>
137    FIL fsrc, fdst;      <span class="c">/* File objects */</span>
138    BYTE buffer[4096];   <span class="c">/* File copy buffer */</span>
139    FRESULT fr;          <span class="c">/* FatFs function common result code */</span>
140    UINT br, bw;         <span class="c">/* File read/write count */</span>
141
142
143    <span class="c">/* Give work areas to each logical drive */</span>
144    <em>f_mount</em>(&amp;fs0, "0:", 0);
145    <em>f_mount</em>(&amp;fs1, "1:", 0);
146
147    <span class="c">/* Open source file on the drive 1 */</span>
148    fr = <em>f_open</em>(&amp;fsrc, "1:file.bin", FA_READ);
149    if (fr) return (int)fr;
150
151    <span class="c">/* Create destination file on the drive 0 */</span>
152    fr = <em>f_open</em>(&amp;fdst, "0:file.bin", FA_WRITE | FA_CREATE_ALWAYS);
153    if (fr) return (int)fr;
154
155    <span class="c">/* Copy source to destination */</span>
156    for (;;) {
157        fr = <em>f_read</em>(&amp;fsrc, buffer, sizeof buffer, &amp;br); <span class="c">/* Read a chunk of data from the source file */</span>
158        if (br == 0) break; <span class="c">/* error or eof */</span>
159        fr = <em>f_write</em>(&amp;fdst, buffer, br, &amp;bw);           <span class="c">/* Write it to the destination file */</span>
160        if (bw &lt; br) break; <span class="c">/* error or disk full */</span>
161    }
162
163    <span class="c">/* Close open files */</span>
164    <em>f_close</em>(&amp;fsrc);
165    <em>f_close</em>(&amp;fdst);
166
167    <span class="c">/* Unregister work area prior to discard it */</span>
168    <em>f_unmount</em>("0:");
169    <em>f_unmount</em>("1:");
170
171    return (int)fr;
172}
173</pre>
174</div>
175
176
177<div class="para ref">
178<h4>See Also</h4>
179<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>
180</div>
181
182
183<p class="foot"><a href="../00index_e.html">Return</a></p>
184</body>
185</html>
186