Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ () — Coherent 3.1.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought


freopen()                 STDIO Function                freopen()




Open file stream for standard I/O

#include <stdio.h>
FILE *freopen (name, type, fp)
char *name, *type; FILE *fp;

freopen  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.

type is a  string that consists of one or  more of the characters
``rwa'' (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 fclose.   freopen is
usually used to change the meaning of stdin, stdout, or stderr.

***** Example *****

This example,  called match.c, looks  in argv[2] for  the pattern
given by  argv[1].  If the  pattern is found, the  line that con-
tains the pattern is written into the file argv[3] or to stdout.


#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 FILE structures can be
allocated per program, including stdin, stdout, and stderr.































COHERENT Lexicon                                           Page 2


Typewritten Software • bear@typewritten.org • Edmonds, WA 98026