| <html lang="en"> |
| <head> |
| <title>fopen - Untitled</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="Untitled"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Stdio.html#Stdio" title="Stdio"> |
| <link rel="prev" href="fmemopen.html#fmemopen" title="fmemopen"> |
| <link rel="next" href="fopencookie.html#fopencookie" title="fopencookie"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="fopen"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="fopencookie.html#fopencookie">fopencookie</a>, |
| Previous: <a rel="previous" accesskey="p" href="fmemopen.html#fmemopen">fmemopen</a>, |
| Up: <a rel="up" accesskey="u" href="Stdio.html#Stdio">Stdio</a> |
| <hr> |
| </div> |
| |
| <h3 class="section">4.17 <code>fopen</code>—open a file</h3> |
| |
| <p><a name="index-fopen-178"></a><a name="index-g_t_005ffopen_005fr-179"></a><strong>Synopsis</strong> |
| <pre class="example"> #include <stdio.h> |
| FILE *fopen(const char *<var>file</var>, const char *<var>mode</var>); |
| |
| FILE *_fopen_r(struct _reent *<var>reent</var>, |
| const char *<var>file</var>, const char *<var>mode</var>); |
| |
| </pre> |
| <p><strong>Description</strong><br> |
| <code>fopen</code> initializes the data structures needed to read or write a |
| file. Specify the file's name as the string at <var>file</var>, and the kind |
| of access you need to the file with the string at <var>mode</var>. |
| |
| <p>The alternate function <code>_fopen_r</code> is a reentrant version. |
| The extra argument <var>reent</var> is a pointer to a reentrancy structure. |
| |
| <p>Three fundamental kinds of access are available: read, write, and append. |
| <code>*</code><var>mode</var> must begin with one of the three characters `<code>r</code>', |
| `<code>w</code>', or `<code>a</code>', to select one of these: |
| |
| <dl> |
| <dt><code>r</code><dd>Open the file for reading; the operation will fail if the file does |
| not exist, or if the host system does not permit you to read it. |
| |
| <br><dt><code>w</code><dd>Open the file for writing <em>from the beginning</em> of the file: |
| effectively, this always creates a new file. If the file whose name you |
| specified already existed, its old contents are discarded. |
| |
| <br><dt><code>a</code><dd>Open the file for appending data, that is writing from the end of |
| file. When you open a file this way, all data always goes to the |
| current end of file; you cannot change this using <code>fseek</code>. |
| </dl> |
| |
| <p>Some host systems distinguish between “binary” and “text” files. |
| Such systems may perform data transformations on data written to, or |
| read from, files opened as “text”. |
| If your system is one of these, then you can append a `<code>b</code>' to any |
| of the three modes above, to specify that you are opening the file as |
| a binary file (the default is to open the file as a text file). |
| |
| <p>`<code>rb</code>', then, means “read binary”; `<code>wb</code>', “write binary”; and |
| `<code>ab</code>', “append binary”. |
| |
| <p>To make C programs more portable, the `<code>b</code>' is accepted on all |
| systems, whether or not it makes a difference. |
| |
| <p>Finally, you might need to both read and write from the same file. |
| You can also append a `<code>+</code>' to any of the three modes, to permit |
| this. (If you want to append both `<code>b</code>' and `<code>+</code>', you can do it |
| in either order: for example, <code>"rb+"</code> means the same thing as |
| <code>"r+b"</code> when used as a mode string.) |
| |
| <p>Use <code>"r+"</code> (or <code>"rb+"</code>) to permit reading and writing anywhere in |
| an existing file, without discarding any data; <code>"w+"</code> (or <code>"wb+"</code>) |
| to create a new file (or begin by discarding all data from an old one) |
| that permits reading and writing anywhere in it; and <code>"a+"</code> (or |
| <code>"ab+"</code>) to permit reading anywhere in an existing file, but writing |
| only at the end. |
| |
| <p><br> |
| <strong>Returns</strong><br> |
| <code>fopen</code> returns a file pointer which you can use for other file |
| operations, unless the file you requested could not be opened; in that |
| situation, the result is <code>NULL</code>. If the reason for failure was an |
| invalid string at <var>mode</var>, <code>errno</code> is set to <code>EINVAL</code>. |
| |
| <p><br> |
| <strong>Portability</strong><br> |
| <code>fopen</code> is required by ANSI C. |
| |
| <p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>, |
| <code>lseek</code>, <code>open</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>. |
| |
| <p><br> |
| |
| </body></html> |
| |