1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>16.11: Redirection (
<TT>freopen
</TT>)
</title>
10 <link href=
"sx2j.html" rev=precedes
>
11 <link href=
"sx3.html" rel=precedes
>
12 <link href=
"sx2.html" rev=subdocument
>
15 <H2>16.11: Redirection (
<TT>freopen
</TT>)
</H2>
18 standard input and standard output are enough,
19 and these programs can get by using just
20 <TT>getchar
</TT>,
<TT>putchar
</TT>,
<TT>printf
</TT>, etc.,
21 and letting any input/output redirection be handled
22 by the user and the operating system
23 (perhaps using command-line redirection such as
<TT><</TT> and
<TT>></TT>).
24 Other programs handle all file manipulation themselves,
25 opening files with
<TT>fopen
</TT> and maintaining file pointer
26 (
<TT>FILE *
</TT>) variables recording the streams to which all
27 input and output is done
28 (with
<TT>getc
</TT>,
<TT>putc
</TT>,
<TT>fprintf
</TT>, etc.).
30 a program has to be rewritten in a hurry,
31 to allow it to read or write a named file
32 without manipulating file pointers and changing
33 every call to
<TT>getchar
</TT> to
<TT>getc
</TT>,
34 every call to
<TT>printf
</TT> to
<TT>fprintf
</TT>, etc.
35 In these cases, the function
<TT>freopen
</TT> comes in handy:
36 it reopens an existing stream on a new file.
39 FILE *freopen(char *filename, char *mode, FILE *fp)
41 <TT>freopen
</TT> is about like
<TT>fopen
</TT>,
42 except that rather than allocating a new stream,
45 the caller-supplied stream
<TT>fp
</TT>.
47 to redirect a program's output to a file ``from within,''
50 freopen(filename,
"w", stdout);
52 </p><p>A disadvantage of
<TT>freopen
</TT>
53 is that there's generally no way to undo it;
55 change your mind later and
56 make
<TT>stdin
</TT> or
<TT>stdout
</TT> go back to where they had been
57 before you called
<TT>freopen
</TT>.
58 In situations where you want to be able to swich back and forth between streams,
59 it's much better if you
<em>can
</em> chase down and
61 every call to
<TT>getchar
</TT> to
<TT>getc
</TT>,
62 every call to
<TT>printf
</TT> to
<TT>fprintf
</TT>, etc.,
63 and then use some
<TT>FILE *
</TT> variable under your control
64 (typically with a name like
<TT>ifp
</TT> or
<TT>ofp
</TT>)
65 so that you can set it
67 to point to a file by calling
<TT>fopen
</TT>,
68 and later back to
<TT>stdin
</TT> or
<TT>stdout
</TT> by simply reassigning it.
72 <a href=
"sx2j.html" rev=precedes
>prev
</a>
73 <a href=
"sx3.html" rel=precedes
>next
</a>
74 <a href=
"sx2.html" rev=subdocument
>up
</a>
75 <a href=
"top.html">top
</a>
78 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
79 //
<a href=
"copyright.html">Copyright
</a> 1996-
1999
80 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>