2ee700453cd3ba43938ddb6f4dad8be7ff170785
[bertos.git] / bertos / fs / fatfs / doc / en / open.html
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=iso-8859-1">\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="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">\r
8 <title>FatFs - f_open</title>\r
9 </head>\r
10 \r
11 <body>\r
12 \r
13 <div class="para">\r
14 <h2>f_open</h2>\r
15 <p>The f_open function creates a <em>file object</em> to be used to access the file.</p>\r
16 <pre>\r
17 FRESULT f_open (\r
18   FIL* <em>FileObject</em>,      /* Pointer to the blank file object structure */\r
19   const char* <em>FileName</em>, /* Pointer to the file neme */\r
20   BYTE <em>ModeFlags</em>        /* Mode flags */\r
21 );\r
22 </pre>\r
23 </div>\r
24 \r
25 <div class="para">\r
26 <h4>Parameters</h4>\r
27 <dl class="par">\r
28 <dt>FileObject</dt>\r
29 <dd>Pointer to the file object structure to be created. After the f_open funciton succeeded, the file can be accessed with the file object structure until it is closed.</dd>\r
30 <dt>FileName</dt>\r
31 <dd>Pointer to a null-terminated string that specifies the <a href="filename.html">file name</a> to create or open.</dd>\r
32 <dt>ModeFlags</dt>\r
33 <dd>Specifies the type of access and open method for the file. It is specified by a combination of following flags.<br>\r
34 <table class="lst">\r
35 <tr><th>Value</th><th>Description</th></tr>\r
36 <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>\r
37 <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>\r
38 <tr><td>FA_OPEN_EXISTING</td><td>Opens the file. The function fails if the file is not existing. (Default)</td></tr>\r
39 <tr><td>FA_OPEN_ALWAYS</td><td>Opens the file, if it is existing. If not, the function creates the new file.</td></tr>\r
40 <tr><td>FA_CREATE_NEW</td><td>Creates a new file. The function fails if the file is already existing.</td></tr>\r
41 <tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file is existing, it is truncated and overwritten.</td></tr>\r
42 </table>\r
43 </dd>\r
44 </dl>\r
45 </div>\r
46 \r
47 \r
48 <div class="para">\r
49 <h4>Return Values</h4>\r
50 <dl class="ret">\r
51 <dt>FR_OK (0)</dt>\r
52 <dd>The function succeeded and the file object is valid.</dd>\r
53 <dt>FR_NO_FILE</dt>\r
54 <dd>Could not find the file.</dd>\r
55 <dt>FR_NO_PATH</dt>\r
56 <dd>Could not find the path.</dd>\r
57 <dt>FR_INVALID_NAME</dt>\r
58 <dd>The file name is invalid.</dd>\r
59 <dt>FR_INVALID_DRIVE</dt>\r
60 <dd>The drive number is invalid.</dd>\r
61 <dt>FR_EXIST</dt>\r
62 <dd>The file is already existing.</dd>\r
63 <dt>FR_DENIED</dt>\r
64 <dd>The required access was denied due to one of the following reasons:\r
65 <ul><li>Write mode open of a read-only file.</li>\r
66 <li>File cannot be created due to a read-only file or same name directory is existing.</li>\r
67 <li>File cannot be created due to the directory table or disk is full.</li></ul></dd>\r
68 <dt>FR_NOT_READY</dt>\r
69 <dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd>\r
70 <dt>FR_WRITE_PROTECTED</dt>\r
71 <dd>Write mode open or creation under the medium is write protected.</dd>\r
72 <dt>FR_DISK_ERR</dt>\r
73 <dd>The function failed due to an error in the disk function.</dd>\r
74 <dt>FR_INT_ERR</dt>\r
75 <dd>The function failed due to a wrong FAT structure or an internal error.</dd>\r
76 <dt>FR_NOT_ENABLED</dt>\r
77 <dd>The logical drive has no work area.</dd>\r
78 <dt>FR_NO_FILESYSTEM</dt>\r
79 <dd>There is no valid FAT partition on the disk.</dd>\r
80 </dl>\r
81 </div>\r
82 \r
83 \r
84 <div class="para">\r
85 <h4>Description</h4>\r
86 <p>The created file object is used for subsequent calls to refer to the file. When close an open file object, use <a href="close.html">f_close</a> function. If modified file is not closed, the file may be collapsed.</p>\r
87 <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>\r
88 <p>The mode flags, <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW, FA_OPEN_ALWAYS</tt>, are not available in read-only configuration.</p>\r
89 </div>\r
90 \r
91 \r
92 <div class="para">\r
93 <h4>Example (File Copy)</h4>\r
94 <pre>\r
95 void main ()\r
96 {\r
97     FATFS fs;            // Work area (file system object) for logical drive\r
98     FIL fsrc, fdst;      // file objects\r
99     BYTE buffer[4096];   // file copy buffer\r
100     FRESULT res;         // FatFs function common result code\r
101     UINT br, bw;         // File R/W count\r
102 \r
103 \r
104     // Register a work area for logical drive 0\r
105     f_mount(0, &amp;fs);\r
106 \r
107     // Open source file\r
108     res = f_open(&amp;fsrc, "srcfile.dat", FA_OPEN_EXISTING | FA_READ);\r
109     if (res) die(res);\r
110 \r
111     // Create destination file\r
112     res = f_open(&amp;fdst, "dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);\r
113     if (res) die(res);\r
114 \r
115     // Copy source to destination\r
116     for (;;) {\r
117         res = f_read(&amp;fsrc, buffer, sizeof(buffer), &amp;br);\r
118         if (res || br == 0) break;   // error or eof\r
119         res = f_write(&amp;fdst, buffer, br, &amp;bw);\r
120         if (res || bw &lt; br) break;   // error or disk full\r
121     }\r
122 \r
123     // Close all files\r
124     f_close(&amp;fsrc);\r
125     f_close(&amp;fdst);\r
126 \r
127     // Unregister a work area before discard it\r
128     f_mount(0, NULL);\r
129 }\r
130 </pre>\r
131 </div>\r
132 \r
133 \r
134 <div class="para">\r
135 <h4>References</h4>\r
136 <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
137 </div>\r
138 \r
139 <p class="foot"><a href="../00index_e.html">Return</a></p>\r
140 </body>\r
141 </html>\r