- Added a memory configuration option. (_USE_LFN) - Added file lock feature. (_FS_SHARE) - Added fast seek feature. (_USE_FASTSEEK) - Changed some types on the API, XCHAR->TCHAR. - Changed fname member in the FILINFO structure on Unicode cfg. - String functions support UTF-8 encoding files on Unicode cfg.
		
			
				
	
	
		
			153 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			153 lines
		
	
	
		
			6.1 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="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>/* Pointer to the blank file object structure */</span>
 | |
|   const TCHAR* <em>FileName</em>, <span>/* Pointer to the file neme */</span>
 | |
|   BYTE <em>ModeFlags</em>         <span>/* 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 if the file is already 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 read-only file or same directory name is existing.</li>
 | |
| <li>File cannot be created due to the directory table or volume 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.</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>/* Work area (file system object) for logical drives */</span>
 | |
|     FIL fsrc, fdst;      <span>/* file objects */</span>
 | |
|     BYTE buffer[4096];   <span>/* file copy buffer */</span>
 | |
|     FRESULT res;         <span>/* FatFs function common result code */</span>
 | |
|     UINT br, bw;         <span>/* File read/write count */</span>
 | |
| 
 | |
| 
 | |
|     <span>/* Register work area for logical drives */</span>
 | |
|     f_mount(0, &fs[0]);
 | |
|     f_mount(1, &fs[1]);
 | |
| 
 | |
|     <span>/* Open source file on the drive 1 */</span>
 | |
|     res = f_open(&fsrc, "1:srcfile.dat", FA_OPEN_EXISTING | FA_READ);
 | |
|     if (res) die(res);
 | |
| 
 | |
|     <span>/* Create destination file on the drive 0 */</span>
 | |
|     res = f_open(&fdst, "0:dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
 | |
|     if (res) die(res);
 | |
| 
 | |
|     <span>/* Copy source to destination */</span>
 | |
|     for (;;) {
 | |
|         res = f_read(&fsrc, buffer, sizeof(buffer), &br);    <span>/* Read a chunk of src file */</span>
 | |
|         if (res || br == 0) break; <span>/* error or eof */</span>
 | |
|         res = f_write(&fdst, buffer, br, &bw);               <span>/* Write it to the dst file */</span>
 | |
|         if (res || bw < br) break; <span>/* error or disk full */</span>
 | |
|     }
 | |
| 
 | |
|     <span>/* Close open files */</span>
 | |
|     f_close(&fsrc);
 | |
|     f_close(&fdst);
 | |
| 
 | |
|     <span>/* 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>
 |