4.3BSD-UWisc/man/cat3/fopen.3s




FOPEN(3S)           UNIX Programmer's Manual            FOPEN(3S)



NAME
     fopen, freopen, fdopen - open a stream

SYNOPSIS
     #include <stdio.h>

     FILE *fopen(filename, type)
     char *filename, *type;

     FILE *freopen(filename, type, stream)
     char *filename, *type;
     FILE *stream;

     FILE *fdopen(fildes, type)
     char *type;

DESCRIPTION
     _F_o_p_e_n opens the file named by _f_i_l_e_n_a_m_e and associates a
     stream with it.  _F_o_p_e_n returns a pointer to be used to iden-
     tify the stream in subsequent operations.

     _T_y_p_e is a character string having one of the following
     values:

     "r"  open for reading

     "w"  create for writing

     "a"  append: open for writing at end of file, or create for
          writing

     In addition, each _t_y_p_e may be followed by a "+" to have the
     file opened for reading and writing.  "r+" positions the
     stream at the beginning of the file, "w+" creates or trun-
     cates it, and "a+" positions it at the end.  Both reads and
     writes may be used on read/write streams, with the limita-
     tion that an _f_s_e_e_k, _r_e_w_i_n_d, or reading an end-of-file must
     be used between a read and a write or vice-versa.

     _F_r_e_o_p_e_n substitutes the named file in place of the open
     _s_t_r_e_a_m.  It returns the original value of _s_t_r_e_a_m.  The ori-
     ginal stream is closed.

     _F_r_e_o_p_e_n is typically used to attach the preopened constant
     names, stdin, stdout, stderr, to specified files.

     _F_d_o_p_e_n associates a stream with a file descriptor obtained
     from _o_p_e_n, _d_u_p, _c_r_e_a_t, or _p_i_p_e(2).  The _t_y_p_e of the stream
     must agree with the mode of the open file.

SEE ALSO
     open(2), fclose(3)



Printed 12/27/86          May 27, 1986                          1






FOPEN(3S)           UNIX Programmer's Manual            FOPEN(3S)



DIAGNOSTICS
     _F_o_p_e_n and _f_r_e_o_p_e_n return the pointer NULL if _f_i_l_e_n_a_m_e cannot
     be accessed, if too many files are already open, or if other
     resources needed cannot be allocated.

BUGS
     _F_d_o_p_e_n is not portable to systems other than UNIX.

     The read/write _t_y_p_e_s do not exist on all systems.  Those
     systems without read/write modes will probably treat the
     _t_y_p_e as if the "+" was not present.  These are unreliable in
     any event.

     In order to support the same number of open files as does
     the system, _f_o_p_e_n must allocate additional memory for data
     structures using _c_a_l_l_o_c after 20 files have been opened.
     This confuses some programs which use their own memory allo-
     cators.  An undocumented routine, _f__p_r_e_a_l_l_o_c, may be called
     to force immediate allocation of all internal memory except
     for buffers.



































Printed 12/27/86          May 27, 1986                          2