ffrreeooppeenn() -- STDIO Function (libc)

Open file 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frreeooppeenn (_n_a_m_e, _t_y_p_e, _f_p)
cchhaarr *_n_a_m_e, *_t_y_p_e; FFIILLEE *_f_p;

ffrreeooppeenn() reinitializes  the file stream  _f_p. It closes  the file currently
associated with it,  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 names a file.

_t_y_p_e is  a string that  consists of one  or more of  the characters ``rrwwaa''
(for, respectively,  read, write, and  append) to indicate the  mode of the
stream.  For  further discussion  of the _t_y_p_e  variable, see the  entry for
ffooppeenn().  ffrreeooppeenn() differs  from  ffooppeenn() only  in that  _f_p specifies  the
stream to be  used.  Any stream previously associated with  _f_p is closed by
ffcclloossee().   ffrreeooppeenn()  is usually  used  to change  the  meaning of  ssttddiinn,
ssttddoouutt, or ssttddeerrrr.

_E_x_a_m_p_l_e
This example,  called mmaattcchh.cc,  looks in aarrggvv[22]  for the pattern  given by
aarrggvv[11].  If  the pattern is found,  the line that contains  the pattern is
written into the file aarrggvv[33] or to ssttddoouutt.

#include <stdio.h>
#define MAXLINE 128
char buffer[MAXLINE];

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

main(argc,argv)
int argc; char *argv[];
{
    FILE *fpin, *fpout;

    if (argc != 3 && argc != 4)
        fatal("Usage: match pattern infile [outfile]");
    if ((fpin = fopen(argv[2], "r")) == NULL)
        fatal("Cannot open input file");

    fpout = stdout;
    if (argc == 4)
        if ((fpout = freopen(argv[3], "w", stdout)) == NULL)
            fatal("Cannot open output file");

    while (fgets(buffer, MAXLINE, fpin) != NULL) {
        if (pnmatch(buffer, argv[1], 1))
            fputs(buffer, stdout);
    }
    exit(0);
}

_S_e_e _A_l_s_o
ffooppeenn(), lliibbcc
ANSI Standard, section 7.9.5.4
POSIX Standard, section 8.1

_D_i_a_g_n_o_s_t_i_c_s
ffrreeooppeenn() returns NULL if the _t_y_p_e string is nonsense or if the file cannot
be  opened.   Currently,  only 20  FFIILLEE  structures  can  be allocated  per
program, including ssttddiinn, ssttddoouutt, and ssttddeerrrr.
