#include <stdio.h> FILE *freopen(const char *name, const char *mode, FILE *oldf);
freopen
function closes the stream associated with the FILE
object addressed by oldf
and then reopens it, using the filename and
open mode specified by name
and mode
.
name
is the external name of the file to be opened. The form of
name
is system dependent. Note that the name
to be opened may be
different from the filename currently associated with oldf
.
The mode
string
defines how the file is used. For more information about
open-mode values, see Open modes .
Portable use of freopen
requires that oldf
identify an
open file. This implementation permits oldf
to reference a
closed file, which permits you to reuse a FILE
pointer by
calling freopen
after fclose
.
freopen
is successful, the value of oldf
is returned. The
FILE
object addressed by oldf
is now associated with the file
specified by name
.
If freopen
is unsuccessful, a NULL FILE
pointer is returned.
Further use of oldf
after an unsuccessful freopen
is not permitted.
freopen
to change the stderr
file, thereby
enabling library diagnostic messages to be redirected:
#include <stdio.h> #include <ctype.h> #include <stdlib.h> main() { FILE *f; char answer; puts("Do you wish to save library messages on disk? (Y or N)?"); answer = toupper(getchar()); if (answer == 'Y'){ f = freopen("tso:saved.messages", "w", stderr); if (!f){ puts("Failed to reopen stderr."); exit(EXIT_FAILURE); } } /* The following fopen() is deliberately invalid and causes */ /* a library diagnostic to be written to stderr. */ f = fopen("tso:impossible.file.name", "w"); exit(EXIT_SUCCESS); }
afreopen
, fopen
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.