* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx2k.html
blob8c9c20e5a4513278d9064c0ddea62bbf9265a0f5
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
15 <H2>16.11: Redirection (<TT>freopen</TT>)</H2>
17 <p>For some programs,
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>&lt;</TT> and <TT>&gt;</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.).
29 </p><p>Occasionally,
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.
37 The prototype is
38 <pre>
39 FILE *freopen(char *filename, char *mode, FILE *fp)
40 </pre>
41 <TT>freopen</TT> is about like <TT>fopen</TT>,
42 except that rather than allocating a new stream,
43 it uses
44 (and returns)
45 the caller-supplied stream <TT>fp</TT>.
46 For example,
47 to redirect a program's output to a file ``from within,''
48 you could call
49 <pre>
50 freopen(filename, "w", stdout);
51 </pre>
52 </p><p>A disadvantage of <TT>freopen</TT>
53 is that there's generally no way to undo it;
54 you can't
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
60 change
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.
69 </p><hr>
70 <p>
71 Read sequentially:
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>
76 </p>
77 <p>
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>
81 </p>
82 </body>
83 </html>