annotate docs/ccguide/fopen.refentry @ 554:afff0087c27f

findstr and signal finished.
author roug
date Tue, 22 Oct 2002 19:09:04 +0000
parents fcb97f0ba24b
children 253b6096ee01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
1 <refentry id="fopen">
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
2 <refnamediv>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
3 <refname>fopen</refname>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
4 <refpurpose>Placeholder</refpurpose>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
5 </refnamediv>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
6
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
7 <refsynopsisdiv>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
8 <funcsynopsis>
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
9 <funcsynopsisinfo>
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
10 #include &lt;stdio.h&gt;
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
11 </funcsynopsisinfo>
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
12 <funcprototype>
552
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
13 <funcdef>FILE *<function>fopen</function></funcdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
14 <paramdef>char *<parameter>filename</parameter></paramdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
15 <paramdef>char *<parameter>action</parameter></paramdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
16 </funcprototype>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
17
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
18 <funcprototype>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
19 <funcdef>FILE *<function>freopen</function></funcdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
20 <paramdef>char *<parameter>filename</parameter></paramdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
21 <paramdef>char *<parameter>action</parameter></paramdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
22 <paramdef>FILE *<parameter>streak</parameter></paramdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
23 </funcprototype>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
24
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
25 <funcprototype>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
26 <funcdef>FILE *<function>fdopen</function></funcdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
27 <paramdef>FILE *<parameter>filedes</parameter></paramdef>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
28 <paramdef>char *<parameter>action</parameter></paramdef>
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
29 </funcprototype>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
30 </funcsynopsis>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
31
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
32 </refsynopsisdiv>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
33
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
34 <refsect1><title>Description</title>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
35 <para>
552
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
36 Fopen returns a pointer to a file structure (file pointer) if
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
37 the file name in the string pointed to by "filename" can be
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
38 validly opened with the action in the string pointed to by
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
39 "action".
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
40 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
41 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
42 The valid actions are:
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
43 <informaltable frame="none">
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
44 <tgroup cols="2">
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
45 <colspec colwidth="1in">
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
46 <tbody>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
47 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
48 <entry><quote>r</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
49 <entry>open for reading</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
50 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
51 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
52 <entry><quote>w</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
53 <entry>create for writing</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
54 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
55 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
56 <entry><quote>a</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
57 <entry>append(write) at end of file, or create for writing</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
58 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
59 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
60 <entry><quote>r+</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
61 <entry>open for update</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
62 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
63 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
64 <entry><quote>w+</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
65 <entry>create for update</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
66 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
67 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
68 <entry><quote>a+</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
69 <entry>create or open for update at end of file</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
70 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
71 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
72 <entry><quote>d</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
73 <entry>directory read</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
74 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
75 </tbody>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
76 </tgroup>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
77 </informaltable>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
78 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
79 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
80 Any action may have an <quote>x</quote> after the initial letter which
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
81 indicates to <quote>fopen()</quote> that it should look in the current
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
82 execution directory if a full path is not given, and
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
83 the x also specifies that the file should have execute permission.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
84 <informalexample>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
85 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
86 E.g. f = fopen(<quote>fred</quote>,<quote>wx</quote>);
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
87 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
88 </informalexample>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
89 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
90 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
91 Opening for write will perform a <quote>creat()</quote>. If a file with the
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
92 same name exists when the file is opened for write, it will be truncated
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
93 to zero length. Append means open for write and
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
94 position to the end of the file. Writes to the file via
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
95 <quote>putc()</quote> etc. will extend the file. Only if the file does not
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
96 already exist will it be created.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
97 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
98 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
99 NOTE that the type of a file structure is pre-defined in
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
100 <quote>stdio.h</quote> as FILE, so that a user program may decale or define
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
101 a file pointer by, for example, FILE *f;
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
102 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
103 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
104 Three file pointers are available and can be considered open
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
105 the moment the program runs:
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
106
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
107 <informaltable frame="none">
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
108 <tgroup cols="2">
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
109 <colspec colwidth="1in">
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
110 <tbody>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
111 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
112 <entry>stdin</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
113 <entry>the standard input - equivalent to path number 0</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
114 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
115 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
116 <entry>stdout</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
117 <entry>the standard output - equivalent to path number 1</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
118 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
119 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
120 <entry>stderr</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
121 <entry>the standard error output - equivalent to path number 2</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
122 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
123 </tbody>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
124 </tgroup>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
125 </informaltable>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
126 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
127 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
128 All files are automatically buffered except stderr, unless a
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
129 file is made unbuffered by a call to setbuf() (q.v.).
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
130 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
131 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
132 Freopen is usually used to attach stdin, stdout, and stderr to
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
133 specified files. Freopen substitutes the file passed to it
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
134 instead of the open stream. The original stream is closed.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
135 NOTE that the original stream will be closed even if the open
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
136 does not succeed.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
137 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
138 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
139 Fdopen associates a stream with a file descriptor. The streams
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
140 type(r,w,a) must be the same as the mode of the open file.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
141 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
142 </refsect1>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
143
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
144 <refsect1><title>Caveats</title>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
145 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
146 The <quote>action</quote> passed as an argument to fopen must be a pointer
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
147 to a string, <emphasis>not</emphasis> a character. For example
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
148 <literallayout>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
149 fp = fopen(<quote>fred</quote>,<quote>r</quote>); is correct but
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
150 fp = fopen(<quote>fred</quote>,'r'); is not.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
151 </literallayout>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
152 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
153 </refsect1>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
154
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
155 <refsect1><title>Diagnostics</title>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
156 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
157 Fopen returns NULL (0) if the call was unsuccessful.
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
158 </para>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
159 </refsect1>
542
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
160
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
161 <refsect1><title>See Also</title>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
162 <para>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
163 System call
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
164 <link linkend="open">open()</link>,
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
165 <link linkend="fflush">fclose()</link>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
166 </para>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
167 </refsect1>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
168
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
169 </refentry>