

freopen()                 STDIO Function                freopen()




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 fp.   It closes  the file
currently associated with it, opens or creates the file name, and
returns  a  pointer  to the  structure  for  use  by other  STDIO
routines.  name 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 type vari-
able, see  the entry for fopen.  freopen  differs from fopen only
in  that  fp  specifies  the  stream  to  be  used.   Any  stream
previously associated  with fp 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.

***** Example *****

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 con-
tains 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");




COHERENT Lexicon                                           Page 1




freopen()                 STDIO Function                freopen()





        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);
}


***** See Also *****

fopen(), STDIO

***** Diagnostics *****

freopen returns  NULL if  the type 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.































COHERENT Lexicon                                           Page 2


