ffooppeenn() -- STDIO Function (libc)

Open a stream for standard I/O
#iinncclluuddee <ssttddiioo.hh>
FFIILLEE *ffooppeenn (_n_a_m_e, _t_y_p_e) cchhaarr *_n_a_m_e, *_t_y_p_e;

ffooppeenn() allocates  and initializes  a FFIILLEE  structure, or _s_t_r_e_a_m;  opens or
creates the  file _n_a_m_e; and returns  a pointer to the  structure for use by
other STDIO routines.  _n_a_m_e refers to the file to be opened.

_t_y_p_e is a string that consists of one or more of the characters ``rwa'', to
indicate the mode of the string, as follows:

    rr   Read; error if file not found
    ww   Write; truncate if found, create if not found

    aa   Append to end of file; no truncation, create if not found
    rr+  Read and write; no truncation, error if not found

    ww+  Write and read; truncate if found, create if not found
    aa+  Append and read; no truncation, create if not found

The modes that contain `a' set  the seek pointer to point at the end of the
file; all other modes set it  to point at the beginning of the file.  Modes
that contain `+' both read and write; however, a program must call ffsseeeekk or
rreewwiinndd before it switches from reading to writing or vice versa.


_E_x_a_m_p_l_e
This  example   copies  aarrggvv[11]  to  aarrggvv[22]   using  STDIO  routines.   It
demonstrates  the  functions  ffooppeenn(),  ffrreeaadd(),  ffwwrriittee(),  ffcclloossee(),  and
ffeeooff().

#include <stdio.h>
#include <stdlib.h>
/* BUFSIZ is defined in stdio.h */
char buf[BUFSIZ];

void fatal(message)
char *message;
{
    fprintf(stderr, "copy: %s\n", message);
    exit(1);
}

main(argc, argv)
int argc; char *argv[];
{
    register FILE *ifp, *ofp;
    register unsigned int n;

    if (argc != 3)
        fatal("Usage: copy source destination");
    if ((ifp = fopen(argv[1], "r")) == NULL)
        fatal("cannot open input file");
    if ((ofp = fopen(argv[2], "w")) == NULL)
        fatal("cannot open output file");

    while ((n = fread(buf, 1, BUFSIZ, ifp)) != 0) {
        if (fwrite(buf, 1, n, ofp) != n)
            fatal("write error");
    }

    if (!feof(ifp))
        fatal("read error");
    if (fclose(ifp) == EOF || fclose(ofp) == EOF)
        fatal("cannot close");
    exit(0);
}

_S_e_e _A_l_s_o
ffcclloossee(), ffddooppeenn(), ffrreeooppeenn(), lliibbcc
ANSI Standard, section 7.9.5.3
POSIX Standard, section 8.1

_D_i_a_g_n_o_s_t_i_c_s
ffooppeenn() returns  NULL if it cannot  allocate a FFIILLEE structure,  if the _t_y_p_e
string is nonsense, or if the call to ooppeenn() or ccrreeaatt() fails.

The header file ssttddiioo.hh defines the manifest constant FFOOPPEENN_MMAAXX, which sets
the maximum  number of FFIILLEE  structures that you can  allocate per program,
including ssttddiinn,  ssttddoouutt, and ssttddeerrrr. For release 4.2,  FFOOPPEENN_MMAAXX is set to
60.

_N_o_t_e_s
Many operating systems recognize a  `b' modifier to the _t_y_p_e argument; this
indicates that the file contains binary information, and lets the operating
system handle ``funny characters'' correctly.  COHERENT has no need of such
a  modifier, so  if  you append  `b'  to _t_y_p_e,  it will  be ignored.   This
modifier,  however,  is recognized  by  numerous  other operating  systems,
including MS-DOS,  OS/2, and GEMDOS.  If you expect  to port developed code
to any of these operating systems, files should append the `b' to _t_y_p_e.
