]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/FileSystem/FatFs-0.7e/doc/en/open.html
33d6d9cf2b842cbfa38c005f4548ba216696e328
[freertos] / FreeRTOS / Demo / Common / FileSystem / FatFs-0.7e / 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 XCHAR* <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.</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, a new file is created.</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 volume 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>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 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 </div>\r
89 \r
90 \r
91 <div class="para">\r
92 <h4>QuickInfo</h4>\r
93 <p>Always available. The mode flags, <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW, FA_OPEN_ALWAYS</tt>, are not available when <tt>_FS_READONLY == 1</tt>.</p>\r
94 </div>\r
95 \r
96 \r
97 <div class="para">\r
98 <h4>Example (File Copy)</h4>\r
99 <pre>\r
100 void main (void)\r
101 {\r
102     FATFS fs[2];         /* Work area (file system object) for logical drives */\r
103     FIL fsrc, fdst;      /* file objects */\r
104     BYTE buffer[4096];   /* file copy buffer */\r
105     FRESULT res;         /* FatFs function common result code */\r
106     UINT br, bw;         /* File R/W count */\r
107 \r
108 \r
109     /* Register work area for logical drives */\r
110     f_mount(0, &amp;fs[0]);\r
111     f_mount(1, &amp;fs[1]);\r
112 \r
113     /* Open source file on the drive 1 */\r
114     res = f_open(&amp;fsrc, "1:srcfile.dat", FA_OPEN_EXISTING | FA_READ);\r
115     if (res) die(res);\r
116 \r
117     /* Create destination file on the drive 0 */\r
118     res = f_open(&amp;fdst, "0:dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);\r
119     if (res) die(res);\r
120 \r
121     /* Copy source to destination */\r
122     for (;;) {\r
123         res = f_read(&amp;fsrc, buffer, sizeof(buffer), &amp;br);\r
124         if (res || br == 0) break;   /* error or eof */\r
125         res = f_write(&amp;fdst, buffer, br, &amp;bw);\r
126         if (res || bw &lt; br) break;   /* error or disk full */\r
127     }\r
128 \r
129     /* Close open files */\r
130     f_close(&amp;fsrc);\r
131     f_close(&amp;fdst);\r
132 \r
133     /* Unregister work area prior to discard it */\r
134     f_mount(0, NULL);\r
135     f_mount(1, NULL);\r
136 }\r
137 </pre>\r
138 </div>\r
139 \r
140 \r
141 <div class="para">\r
142 <h4>See Also</h4>\r
143 <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
144 </div>\r
145 \r
146 <p class="foot"><a href="../00index_e.html">Return</a></p>\r
147 </body>\r
148 </html>\r