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