FatFS/doc/en/open.html
savelij13 f76dc3a97f fatfs v0.03a Dec 11, 2006:
- Improved cluster scan algolithm to write files fast.
- Fixed f_mkdir() creates incorrect directory on FAT32.
2025-09-11 08:59:00 +03:00

134 lines
5.6 KiB
HTML

<!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="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 or opens the file and initialize a file object structure to be used to access the file.</p>
<pre>
FRESULT f_open (
FIL* <em>FileObject</em>, // Pointer to the file object structure
const char* <em>FileName</em>, // Pointer to the file neme
BYTE <em>ModeFlags</em> // Mode flags
);
</pre>
</div>
<div class="para">
<h4>Parameters</h4>
<dl class="par">
<dt>FileObject</dt>
<dd>Pointer to the blank file object structure to be initialized. After the f_open() succeeded, the file can be accessed with the file object structure until it is closed.</dd>
<dt>FileName</dt>
<dd>Pointer to a null-terminated string specifies the full-path file name to create or open. The directory separator is <tt>'/'</tt>. Because the FatFs module does not have a concept of current directory, a full-path name that followed from the root directory must be used. Leading space charactors are skipped if exist, and heading <tt>'/'</tt> can be exist or omitted.</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.</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>
<tr><td>FA_OPEN_ALWAYS</td><td>Opens the file, if it is existing. If not, the function creates the new file.</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. The <tt><em>FileObject</em></tt> structure is used for subsequent calls to refer to the file. When close the file, use function <tt><a href="close.html">f_close()</a></tt>.</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_DENIED</dt>
<dd>The required access was denied due to any of following reasons: write mode open of a file that has read-only attribute, file creation under existing a same name directory or read-only file, cannot be created due to the directory table or disk full.</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_RW_ERROR</dt>
<dd>Any error occured in low level disk I/O.</dd>
<dt>FR_INCORRECT_DISK_CHANGE</dt>
<dd>Incorrect disk removal/change has occured.</dd>
<dt>FR_NOT_ENABLED</dt>
<dd>FatFs module is not enabled.</dd>
<dt>FR_NO_FILESYSTEM</dt>
<dd>There is no valid FAT partition on the disk.</dd>
</dl>
</div>
<div class="para">
<h4>Description</h4>
<p>To start FatFs module, prepare a work area (<tt>FATFS</tt> structure), fill it with zero and set its address to the global variable <em>FatFs</em> to give the work area to the FatFs module. All file functions can work after the initialization. <a href="mountdrv.html">f_mountdrv()</a> can also be used instead of the zero filling, however, the physical drive must be ready at that time. To terminate use of the FatFs module, close all files, clear <em>FatFs</em> and then the work area can be discarded.</p>
<p>Flags <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_OPEN_ALWAYS</tt> are not supported in read-only configuration.</p>
</div>
<div class="para">
<h4>Example (File Copy)</h4>
<pre>
void main ()
{
FATFS fs; // FatFs work area
FIL fsrc, fdst; // file structures
BYTE buffer[4096]; // file copy buffer
FRESULT res; // FatFs function common result code
WORD br, bw; // File R/W count
// Give a work area to FatFs module (activate FatFs module)
FatFs = &fs;
memset(&fs, 0, sizeof(FATFS)); // Clear work area. f_mountdrv() can also be used instead.
// Open source file
res = f_open(&fsrc, "srcfile.dat", FA_OPEN_EXISTING | FA_READ);
if (res) die(res);
// Create destination file
res = f_open(&fdst, "dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
if (res) die(res);
// Copy source to destination
for (;;) {
res = f_read(&fsrc, buffer, sizeof(buffer), &br);
if (res || br == 0) break; // error or eof
res = f_write(&fdst, buffer, br, &bw);
if (res || bw &lt; br) break; // error or disk full
}
// Close all files
f_close(&fsrc);
f_close(&fdst);
// Deactivate FatFs module
FatFs = NULL;
}
</pre>
</div>
<div class="para">
<h4>References</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>