


FOPEN(3V)              C LIBRARY FUNCTIONS              FOPEN(3V)



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(fd, type)
     int fd;
     char *type;

DESCRIPTION
     fopen() opens the file named by _f_i_l_e_n_a_m_e  and  associates  a
     stream  with  it.   If  the open succeeds, fopen() returns a
     pointer to be used to  identify  the  stream  in  subsequent
     operations.

     _f_i_l_e_n_a_m_e points to a character string that contains the name
     of the file to be opened.

     _t_y_p_e is a character  string  having  one  of  the  following
     values:

          r         open for reading

          w         truncate or create for writing

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

          r+        open for update (reading and writing)

          w+        truncate or create for update

          a+        append; open or create for update at EOF

     freopen() opens the file named by  _f_i_l_e_n_a_m_e  and  associates
     the  stream pointed to by _s_t_r_e_a_m with it.  The _t_y_p_e argument
     is used just as in fopen.  The original  stream  is  closed,
     regardless  of whether the open ultimately succeeds.  If the
     open succeeds,  freopen()  returns  the  original  value  of
     _s_t_r_e_a_m.

     freopen() is typically used to attach the preopened  streams
     associated with stdin, stdout, and stderr to other files.




Sun Release 4.1   Last change: 21 January 1990                  1






FOPEN(3V)              C LIBRARY FUNCTIONS              FOPEN(3V)



     fdopen() associates a stream with the  file  descriptor  _f_d.
     File  descriptors  are  obtained  from  calls like open(2V),
     dup(2V), creat(2V), or pipe(2V), which open files but do not
     return streams.  Streams are necessary input for many of the
     Section 3S library routines.  The _t_y_p_e of  the  stream  must
     agree with the access permissions of the open file.

     When a file is opened for update, both input and output  may
     be done on the resulting stream.  However, output may not be
     directly followed by input without an intervening  fseek(3S)
     or  rewind(), and input may not be directly followed by out-
     put without an intervening fseek(), rewind(),  or  an  input
     operation which encounters EOF.

SYSTEM V DESCRIPTION
     When a file is opened for append (that is, when _t_y_p_e is a or
     a+),  it  is  impossible to overwrite information already in
     the file.  fseek()  may  be  used  to  reposition  the  file
     pointer  to  any  position  in  the file, but when output is
     written to the file, the  current  file  pointer  is  disre-
     garded.   All  output  is written at the end of the file and
     causes the file pointer to be repositioned at the end of the
     output.   If  two  separate processes open the same file for
     append, each process may write freely to  the  file  without
     fear  of  destroying output being written by the other.  The
     output from the two processes will be intermixed in the file
     in the order in which it is written.

RETURN VALUES
     On  success,  fopen(),  freopen(),  and  fdopen()  return  a
     pointer  to  FILE  which  identifies  the opened stream.  On
     failure, they return NULL.

SEE ALSO
     open(2V), pipe(2V), fclose(3V), fseek(3S)

BUGS
     In order to support the same number of open files  that  the
     system  does,  fopen()  must  allocate additional memory for
     data structures using calloc()  after  64  files  have  been
     opened.   This  confuses  some  programs which use their own
     memory allocators.













Sun Release 4.1   Last change: 21 January 1990                  2



