blob: 9bf14dc192ca3cbc909ff0e04428e8fc2ebfcf37 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="up" title="FatFs" href="../00index_e.html">
<link rel="alternate" hreflang="ja" title="Japanese" href="../ja/open.html">
<link rel="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">
<title>FatFs - f_open</title>
</head>
<body>
<div class="para">
<h2>f_open</h2>
<p>The f_open function creates a <em>file object</em> to be used to access the file.</p>
<pre>
FRESULT f_open (
FIL* <em>FileObject</em>, <span class="c">/* Pointer to the blank file object structure */</span>
const TCHAR* <em>FileName</em>, <span class="c">/* Pointer to the file neme */</span>
BYTE <em>ModeFlags</em> <span class="c">/* Mode flags */</span>
);
</pre>
</div>
<div class="para">
<h4>Parameters</h4>
<dl class="par">
<dt>FileObject</dt>
<dd>Pointer to the file object structure to be created.</dd>
<dt>FileName</dt>
<dd>Pointer to a null-terminated string that specifies the <a href="filename.html">file name</a> to create or open.</dd>
<dt>ModeFlags</dt>
<dd>Specifies the type of access and open method for the file. It is specified by a combination of following flags.<br>
<table class="lst">
<tr><th>Value</th><th>Description</th></tr>
<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>
<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>
<tr><td>FA_OPEN_EXISTING</td><td>Opens the file. The function fails if the file is not existing. (Default)</td></tr>
<tr><td>FA_OPEN_ALWAYS</td><td>Opens the file if it is existing. If not, a new file is created.<br>
To append data to the file, use f_lseek function after file open in this method.</td></tr>
<tr><td>FA_CREATE_NEW</td><td>Creates a new file. The function fails with FR_EXIST if the file is existing.</td></tr>
<tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file is existing, it is truncated and overwritten.</td></tr>
</table>
</dd>
</dl>
</div>
<div class="para">
<h4>Return Values</h4>
<dl class="ret">
<dt>FR_OK (0)</dt>
<dd>The function succeeded and the file object is valid.</dd>
<dt>FR_NO_FILE</dt>
<dd>Could not find the file.</dd>
<dt>FR_NO_PATH</dt>
<dd>Could not find the path.</dd>
<dt>FR_INVALID_NAME</dt>
<dd>The file name is invalid.</dd>
<dt>FR_INVALID_DRIVE</dt>
<dd>The drive number is invalid.</dd>
<dt>FR_EXIST</dt>
<dd>The file is already existing.</dd>
<dt>FR_DENIED</dt>
<dd>The required access was denied due to one of the following reasons:
<ul><li>Write mode open against a read-only file.</li>
<li>File cannot be created due to a directory or read-only file is existing.</li>
<li>File cannot be created due to the directory table is full.</li></ul></dd>
<dt>FR_NOT_READY</dt>
<dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd>
<dt>FR_WRITE_PROTECTED</dt>
<dd>Write mode open or creation under the medium is write protected.</dd>
<dt>FR_DISK_ERR</dt>
<dd>The function failed due to an error in the disk function.</dd>
<dt>FR_INT_ERR</dt>
<dd>The function failed due to a wrong FAT structure or an internal error.</dd>
<dt>FR_NOT_ENABLED</dt>
<dd>The logical drive has no work area.</dd>
<dt>FR_NO_FILESYSTEM</dt>
<dd>There is no valid FAT volume on the drive.</dd>
<dt>FR_LOCKED</dt>
<dd>The function was rejected due to file shareing policy (_FS_SHARE).</dd>
</dl>
</div>
<div class="para">
<h4>Description</h4>
<p>A file object is created when the function succeeded. The file object is used for subsequent read/write functions to refer to the file. When close an open file object, use <a href="close.html">f_close</a> function. If the modified file is not closed, the file data can be collapsed.</p>
<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>
</div>
<div class="para">
<h4>QuickInfo</h4>
<p>Always available. The mode flags, <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW and FA_OPEN_ALWAYS</tt>, are not available when <tt>_FS_READONLY == 1</tt>.</p>
</div>
<div class="para">
<h4>Example (File Copy)</h4>
<pre>
void main (void)
{
FATFS fs[2]; <span class="c">/* Work area (file system object) for logical drives */</span>
FIL fsrc, fdst; <span class="c">/* file objects */</span>
BYTE buffer[4096]; <span class="c">/* file copy buffer */</span>
FRESULT res; <span class="c">/* FatFs function common result code */</span>
UINT br, bw; <span class="c">/* File read/write count */</span>
<span class="c">/* Register work area for each volume (Always succeeds regardless of disk status) */</span>
f_mount(0, &amp;fs[0]);
f_mount(1, &amp;fs[1]);
<span class="c">/* Open source file on the drive 1 */</span>
res = f_open(&amp;fsrc, "1:srcfile.dat", FA_OPEN_EXISTING | FA_READ);
if (res) die(res);
<span class="c">/* Create destination file on the drive 0 */</span>
res = f_open(&amp;fdst, "0:dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
if (res) die(res);
<span class="c">/* Copy source to destination */</span>
for (;;) {
res = f_read(&amp;fsrc, buffer, sizeof(buffer), &amp;br); <span class="c">/* Read a chunk of src file */</span>
if (res || br == 0) break; <span class="c">/* error or eof */</span>
res = f_write(&amp;fdst, buffer, br, &amp;bw); <span class="c">/* Write it to the dst file */</span>
if (res || bw &lt; br) break; <span class="c">/* error or disk full */</span>
}
<span class="c">/* Close open files */</span>
f_close(&amp;fsrc);
f_close(&amp;fdst);
<span class="c">/* Unregister work area prior to discard it */</span>
f_mount(0, NULL);
f_mount(1, NULL);
}
</pre>
</div>
<div class="para">
<h4>See Also</h4>
<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>
</div>
<p class="foot"><a href="../00index_e.html">Return</a></p>
</body>
</html>