Chapter Contents |
Previous |
Next |
freopen |
Portability: | ISO/ANSI C conforming, UNIX compatible |
SYNOPSIS | |
DESCRIPTION | |
RETURN VALUE | |
EXAMPLE | |
RELATED FUNCTIONS | |
SEE ALSO |
SYNOPSIS |
#include <stdio.h> FILE *freopen(const char *name, const char *mode, FILE *oldf);
DESCRIPTION |
The
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
.
RETURN VALUE |
If
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.
EXAMPLE |
This example uses
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); }
RELATED FUNCTIONS |
SEE ALSO |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.