fix rest of Chris' bugs, mostly sign and padding placement
[unix-history] / usr / src / lib / libc / stdio / fopen.3
CommitLineData
251f9c2d
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
17eb345e 5.\" @(#)fopen.3 6.3 (Berkeley) %G%
251f9c2d 6.\"
cf0272dc 7.TH FOPEN 3S ""
251f9c2d
KM
8.UC 4
9.SH NAME
10fopen, freopen, fdopen \- open a stream
11.SH SYNOPSIS
12.B #include <stdio.h>
13.PP
14.SM
15.B FILE
16.B *fopen(filename, type)
17.br
18.B char *filename, *type;
19.PP
20.SM
21.B FILE
22.B *freopen(filename, type, stream)
23.br
24.B char *filename, *type;
25.br
26.SM
27.B FILE
28.B *stream;
29.PP
30.SM
31.B FILE
32.B *fdopen(fildes, type)
33.br
34.B char *type;
35.SH DESCRIPTION
36.I Fopen
37opens the file named by
38.I filename
39and associates a stream with it.
40.I Fopen
e2703397 41returns a pointer to be used to identify the stream in subsequent operations.
251f9c2d
KM
42.PP
43.I Type
44is a character string having one of the following values:
45.TP 5
46"r"
47open for reading
48.ns
49.TP 5
50"w"
51create for writing
52.ns
53.TP 5
54"a"
e2703397 55append: open for writing at end of file, or create for writing
251f9c2d
KM
56.PP
57In addition, each
58.I type
17eb345e 59may be followed by a "+" to have the file opened for reading and writing.
e2703397 60"r+" positions the stream at the beginning of the file, "w+" creates
251f9c2d
KM
61or truncates it, and "a+" positions it at the end. Both reads and writes
62may be used on read/write streams, with the limitation that an
63.I fseek, rewind,
e2703397 64or reading an end-of-file must be used between a read and a write or vice-versa.
251f9c2d
KM
65.PP
66.I Freopen
e2703397 67substitutes the named file in place of the open
251f9c2d
KM
68.IR stream .
69It returns the original value of
70.IR stream .
71The original stream is closed.
72.PP
73.I Freopen
e2703397 74is typically used to attach the preopened constant names,
251f9c2d
KM
75.B stdin, stdout, stderr,
76to specified files.
77.PP
78.I Fdopen
79associates a stream with a file descriptor obtained from
80.I open, dup, creat,
81or
82.IR pipe (2).
83The
84.I type
85of the stream must agree with the mode of the open file.
86.SH "SEE ALSO"
87open(2),
88fclose(3)
89.SH DIAGNOSTICS
90.I Fopen
91and
92.I freopen
93return the pointer
94.SM
95.B NULL
96if
97.I filename
c719da84
MK
98cannot be accessed,
99if too many files are already open,
100or if other resources needed cannot be allocated.
251f9c2d
KM
101.SH BUGS
102.I Fdopen
103is not portable to systems other than UNIX.
104.PP
105The read/write
106.I types
107do not exist on all systems. Those systems without
108read/write modes will probably treat the
109.I type
17eb345e 110as if the "+" was not present. These are unreliable in any event.
c719da84
MK
111.PP
112In order to support the same number of open files as does the system,
113.I fopen
114must allocate additional memory for data structures using
115.I calloc
116after 20 files have been opened.
117This confuses some programs which use their own memory allocators.
118An undocumented routine,
119.IR f_prealloc ,
120may be called to force immediate allocation of all internal memory
121except for buffers.