fopen
stream open functions
You're viewing an older version of this page (#3249). View the current version.
NAME
<span class="Nm" id="fopen">fopen</span>, <span class="Nm" id="fdopen">fdopen</span>, <span class="Nm" id="freopen">freopen</span>
INTERFACE
#include <stdio.h> FILE fopen ( const char * restrict path, const char * restrict mode ) FILE fdopen ( int fildes, const char *mode ) FILE freopen ( const char *path, const char *mode, FILE *stream )
DESCRIPTION
The fopen function opens the file whose name is the string pointed to by \c path and associates a stream with it. \n \n The argument \c mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.): <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> "<span class="Dq"><span class="Li">r</span></span>" </td> <td valign="top"> Open text file for reading. The stream is positioned at the beginning of the file. </td> </tr> <tr> <td valign="top" nowrap> "<span class="Dq"><span class="Li">r+</span></span>" </td> <td valign="top"> Open for reading and writing. The stream is positioned at the beginning of the file. </td> </tr> <tr> <td valign="top" nowrap> "<span class="Dq"><span class="Li">w</span></span>" </td> <td valign="top"> Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. </td> </tr> <tr> <td valign="top" nowrap> "<span class="Dq"><span class="Li">w+</span></span>" </td> <td valign="top"> Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. </td> </tr> <tr> <td valign="top" nowrap> "<span class="Dq"><span class="Li">a</span></span>" </td> <td valign="top"> Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek (3) or similar. </td> </tr> <tr> <td valign="top" nowrap> "<span class="Dq"><span class="Li">a+</span></span>" </td> <td valign="top"> Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek (3) or similar. </td> </tr> </table> \n \n The \c mode string can also include the letter ``b'' either as a third character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with and has no effect; the ``b'' is ignored. \n \n Any created files will have mode " <span class="Dv">S_IRUSR</span> | <span class="Dv">S_IWUSR</span> | <span class="Dv">S_IRGRP</span> | <span class="Dv">S_IWGRP</span> | <span class="Dv">S_IROTH</span> | <span class="Dv">S_IWOTH</span> " (<span class="Pq"><span class="Li">0666</span>,</span>) as modified by the process' umask value (see reference:umask (2) ). \n \n Reads and writes may be intermixed on read/write streams in any order, and do not require an intermediate seek as in previous versions of <span class="Em">stdio</span>. This is not portable to other systems, however; <span class="Tn">ANSI</span> <span class="Tn">C</span> requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. \n \n The fdopen function associates a stream with the existing file descriptor, \c fildes. The mode of the stream must be compatible with the mode of the file descriptor. When the stream is closed via fclose (3) , \c fildes is closed also. \n \n The freopen function opens the file whose name is the string pointed to by \c path and associates the stream pointed to by \c stream with it. The original stream (if it exists) is closed. The \c mode argument is used just as in the fopen function. \n \n If the \c path argument is <span class="Dv">NULL</span>, freopen attempts to re-open the file associated with \c stream with a new mode. The new mode must be compatible with the mode that the stream was originally opened with:
- Streams originally opened with mode "<span class="Dq"><span class="Li">r</span></span>" can only be reopened with that same mode.
- Streams originally opened with mode "<span class="Dq"><span class="Li">a</span></span>" can be reopened with the same mode, or mode "<span class="Dq"><span class="Li">w</span>.</span>"
- Streams originally opened with mode "<span class="Dq"><span class="Li">w</span></span>" can be reopened with the same mode, or mode "<span class="Dq"><span class="Li">a</span>.</span>"
- Streams originally opened with mode "<span class="Dq"><span class="Li">r+</span>,</span>" "<span class="Dq"><span class="Li">w+</span>,</span>" or "<span class="Dq"><span class="Li">a+</span></span>" can be reopened with any mode.
\n \n The primary use of the freopen function is to change the file associated with a standard text stream ( <span class="Dv">stderr</span>, <span class="Dv">stdin</span>, or <span class="Dv">stdout</span>).
RETURN VALUES
Upon successful completion fopen , fdopen and freopen return a <span class="Tn">FILE</span> pointer. Otherwise, <span class="Dv">NULL</span> is returned and the global variable <span class="Va">errno</span> is set to indicate the error.
ERRORS
<table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EINVAL</span></span>] </td> <td valign="top"> The \c mode argument to fopen , fdopen , or freopen was invalid. </td> </tr> </table> \n \n The fopen , fdopen and freopen functions may also fail and set <span class="Va">errno</span> for any of the errors specified for the routine malloc (3) . \n \n The fopen function may also fail and set <span class="Va">errno</span> for any of the errors specified for the routine reference:open (2) . \n \n The fdopen function may also fail and set <span class="Va">errno</span> for any of the errors specified for the routine reference:fcntl (2) . \n \n The freopen function may also fail and set <span class="Va">errno</span> for any of the errors specified for the routines reference:open (2) , fclose (3) and fflush (3) .
SEE ALSO
reference:open (2) , fclose (3) , reference:fileno (3) , fseek (3) , reference:funopen (3)
STANDARDS
The fopen and freopen functions conform to The fdopen function conforms to