annotate docs/ccguide/fopen.refentry @ 2201:e64d2a5b8c87

Added DW3
author boisy
date Sat, 14 Mar 2009 18:51:35 +0000
parents a4a6b6315146
children
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>
595
e50380625be6 All refentries finished.
roug
parents: 570
diff changeset
3 <refname>Fopen</refname>
570
253b6096ee01 All function prototypes are added.
roug
parents: 552
diff changeset
4 <refpurpose>open a file and return a file pointer</refpurpose>
466
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">
635
a1a6feedb1cc Convert to Docbook XML format
roug
parents: 595
diff changeset
45 <colspec colwidth="0.5in"/>
a1a6feedb1cc Convert to Docbook XML format
roug
parents: 595
diff changeset
46 <colspec colwidth="3.0in"/>
552
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
47 <tbody>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
48 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
49 <entry><quote>r</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
50 <entry>open for reading</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
51 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
52 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
53 <entry><quote>w</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
54 <entry>create for writing</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
55 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
56 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
57 <entry><quote>a</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
58 <entry>append(write) at end of file, or create for writing</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
59 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
60 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
61 <entry><quote>r+</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
62 <entry>open for update</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
63 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
64 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
65 <entry><quote>w+</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
66 <entry>create for update</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
67 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
68 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
69 <entry><quote>a+</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
70 <entry>create or open for update at end of file</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
71 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
72 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
73 <entry><quote>d</quote></entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
74 <entry>directory read</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
75 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
76 </tbody>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
77 </tgroup>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
78 </informaltable>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
79 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
80 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
81 Any action may have an <quote>x</quote> after the initial letter which
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
82 indicates to <quote>fopen()</quote> that it should look in the current
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
83 execution directory if a full path is not given, and
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
84 the x also specifies that the file should have execute permission.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
85 <informalexample>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
86 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
87 E.g. f = fopen(<quote>fred</quote>,<quote>wx</quote>);
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
88 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
89 </informalexample>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
90 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
91 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
92 Opening for write will perform a <quote>creat()</quote>. If a file with the
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
93 same name exists when the file is opened for write, it will be truncated
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
94 to zero length. Append means open for write and
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
95 position to the end of the file. Writes to the file via
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
96 <quote>putc()</quote> etc. will extend the file. Only if the file does not
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
97 already exist will it be created.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
98 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
99 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
100 NOTE that the type of a file structure is pre-defined in
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
101 <quote>stdio.h</quote> as FILE, so that a user program may decale or define
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
102 a file pointer by, for example, FILE *f;
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
103 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
104 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
105 Three file pointers are available and can be considered open
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
106 the moment the program runs:
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
107
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
108 <informaltable frame="none">
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
109 <tgroup cols="2">
655
a4a6b6315146 Completely finished
roug
parents: 635
diff changeset
110 <colspec colwidth="0.6in"/>
a4a6b6315146 Completely finished
roug
parents: 635
diff changeset
111 <colspec colwidth="3.4in"/>
552
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
112 <tbody>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
113 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
114 <entry>stdin</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
115 <entry>the standard input - equivalent to path number 0</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
116 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
117 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
118 <entry>stdout</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
119 <entry>the standard output - equivalent to path number 1</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
120 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
121 <row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
122 <entry>stderr</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
123 <entry>the standard error output - equivalent to path number 2</entry>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
124 </row>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
125 </tbody>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
126 </tgroup>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
127 </informaltable>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
128 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
129 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
130 All files are automatically buffered except stderr, unless a
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
131 file is made unbuffered by a call to setbuf() (q.v.).
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
132 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
133 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
134 Freopen is usually used to attach stdin, stdout, and stderr to
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
135 specified files. Freopen substitutes the file passed to it
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
136 instead of the open stream. The original stream is closed.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
137 NOTE that the original stream will be closed even if the open
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
138 does not succeed.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
139 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
140 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
141 Fdopen associates a stream with a file descriptor. The streams
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
142 type(r,w,a) must be the same as the mode of the open file.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
143 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
144 </refsect1>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
145
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
146 <refsect1><title>Caveats</title>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
147 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
148 The <quote>action</quote> passed as an argument to fopen must be a pointer
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
149 to a string, <emphasis>not</emphasis> a character. For example
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
150 <literallayout>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
151 fp = fopen(<quote>fred</quote>,<quote>r</quote>); is correct but
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
152 fp = fopen(<quote>fred</quote>,'r'); is not.
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
153 </literallayout>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
154 </para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
155 </refsect1>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
156
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
157 <refsect1><title>Diagnostics</title>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
158 <para>
fcb97f0ba24b All diagnostics added.
roug
parents: 542
diff changeset
159 Fopen returns NULL (0) if the call was unsuccessful.
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
160 </para>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
161 </refsect1>
542
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
162
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
163 <refsect1><title>See Also</title>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
164 <para>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
165 System call
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
166 <link linkend="open">open()</link>,
570
253b6096ee01 All function prototypes are added.
roug
parents: 552
diff changeset
167 <link linkend="fclose">fclose()</link>
542
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
168 </para>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
169 </refsect1>
73319ee6a57e All "See Also" implemented
roug
parents: 468
diff changeset
170
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
171 </refentry>