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