COHERENT manpages
This page displays the COHERENT manpage for freopen() [Open file stream for standard I/O].
List of available manpages
Index
freopen() -- STDIO Function (libc)
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 variable, 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 contains 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");
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(),
libc
ANSI Standard, §7.9.5.4
POSIX Standard, §8.1
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.