xref: /csrg-svn/lib/libc/stdio/fopen.3 (revision 61180)
1*61180Sbostic.\" Copyright (c) 1990, 1991, 1993
2*61180Sbostic.\"	The Regents of the University of California.  All rights reserved.
320391Smckusick.\"
446078Sbostic.\" This code is derived from software contributed to Berkeley by
550295Sbostic.\" Chris Torek and the American National Standards Committee X3,
650295Sbostic.\" on Information Processing Systems.
750295Sbostic.\"
846078Sbostic.\" %sccs.include.redist.man%
946078Sbostic.\"
10*61180Sbostic.\"     @(#)fopen.3	8.1 (Berkeley) 06/04/93
1146078Sbostic.\"
1248352Scael.Dd
1348352Scael.Dt FOPEN 3
1448352Scael.Os
1548352Scael.Sh NAME
1648352Scael.Nm fopen ,
1748352Scael.Nm fdopen ,
1848352Scael.Nm freopen
1948352Scael.Nd stream open functions
2048352Scael.Sh SYNOPSIS
2148352Scael.Fd #include <stdio.h>
2248352Scael.Ft FILE *
2348352Scael.Fn fopen "char *path" "char *mode"
2448352Scael.Ft FILE *
2548352Scael.Fn fdopen "int fildes" "char *mode"
2648352Scael.Ft FILE *
2748352Scael.Fn freopen "char *path" "char *mode" "FILE *stream"
2848352Scael.Sh DESCRIPTION
2948352ScaelThe
3048352Scael.Fn fopen
3148352Scaelfunction
3248352Scaelopens the file whose name is the string pointed to by
3348352Scael.Fa path
3448352Scaeland associates a stream with it.
3548352Scael.Pp
3648352ScaelThe argument
3748352Scael.Fa mode
3848352Scaelpoints to a string beginning with one of the following
3948352Scaelsequences (Additional characters may follow these sequences.):
4048352Scael.Bl -tag -width indent
4148352Scael.It Dq Li r
4248352ScaelOpen text file for reading.
4346078SbosticThe stream is positioned at the beginning of the file.
4448352Scael.It Dq Li r+
4546078SbosticOpen for reading and writing.
4646078SbosticThe stream is positioned at the beginning of the file.
4748352Scael.It Dq Li w
4848352ScaelTruncate file to zero length or create text file for writing.
4946078SbosticThe stream is positioned at the beginning of the file.
5048352Scael.No It Dq Li w+
5146078SbosticOpen for reading and writing.
5246078SbosticThe file is created if it does not exist, otherwise it is truncated.
5346078SbosticThe stream is positioned at the beginning of the file.
5448352Scael.It Dq Li a
5546078SbosticOpen for writing.
5646078SbosticThe file is created if it does not exist.
5746078SbosticThe stream is positioned at the end of the file.
5848352Scael.It Dq Li a+
5946078SbosticOpen for reading and writing.
6046078SbosticThe file is created if it does not exist.
6146078SbosticThe stream is positioned at the end of the file.
6248352Scael.El
6348352Scael.Pp
6446078SbosticThe
6548352Scael.Fa mode
6646078Sbosticstring can also include the letter ``b'' either as a third character or
6746078Sbosticas a character between the characters in any of the two-character strings
6846078Sbosticdescribed above.
6948352ScaelThis is strictly for compatibility with
7048352Scael.St -ansiC
7146078Sbosticand has no effect; the ``b'' is ignored.
7248352Scael.Pp
7348352ScaelAny created files will have mode
7448352Scael.Pf \\*q Dv S_IRUSR
7548352Scael\&|
7648352Scael.Dv S_IWUSR
7748352Scael\&|
7848352Scael.Dv S_IRGRP
7948352Scael\&|
8048352Scael.Dv S_IWGRP
8148352Scael\&|
8248352Scael.Dv S_IROTH
8348352Scael\&|
8448352Scael.Dv S_IWOTH Ns \\*q
8548352Scael.Pq Li 0666 ,
8648352Scaelas modified by the process'
8747010Sbosticumask value (see
8848352Scael.Xr umask 2 ) .
8948352Scael.Pp
9046078SbosticReads and writes may be intermixed on read/write streams in any order,
9148352Scaeland do not require an intermediate seek as in previous versions of
9248352Scael.Em stdio .
9348352ScaelThis is not portable to other systems, however;
9448352Scael.Tn ANSI C
9548352Scaelrequires that
9646078Sbostica file positioning function intervene between output and input, unless
9746078Sbostican input operation encounters end-of-file.
9848352Scael.Pp
9920391SmckusickThe
10048352Scael.Fn fdopen
10148352Scaelfunction associates a stream with the existing file descriptor,
10248352Scael.Fa fildes .
10348352ScaelThe
10448352Scael.Fa mode
10546078Sbosticof the stream must be compatible with the mode of the file descriptor.
10648352Scael.Pp
10748352ScaelThe
10848352Scael.Fn freopen
10948352Scaelfunction
11048352Scaelopens the file whose name is the string pointed to by
11148352Scael.Fa path
11248352Scaeland associates the stream pointed to by
11348352Scael.Fa stream
11448352Scaelwith it.
11548352ScaelThe original stream (if it exists) is closed.
11648352ScaelThe
11748352Scael.Fa mode
11848352Scaelargument is used just as in the
11948352Scael.Xr fopen
12048352Scaelfunction.
12148352ScaelThe primary use of the
12248352Scael.Fn freopen
12348352Scaelfunction
12448352Scaelis to change the file associated with a
12548352Scaelstandard text stream
12648352Scael.Pf ( Em stderr ,
12748352Scael.Em stdin ,
12848352Scaelor
12948352Scael.Em stdout ) .
13048352Scael.Sh RETURN VALUES
13148352ScaelUpon successful completion
13248352Scael.Fn fopen ,
13348352Scael.Fn fdopen
13446078Sbosticand
13548352Scael.Fn freopen
13648352Scaelreturn a
13748352Scael.Tn FILE
13848352Scaelpointer.
13948352ScaelOtherwise,
14048352Scael.Dv NULL
14148352Scaelis returned and the global variable
14248352Scael.Va errno
14346078Sbosticis set to indicate the error.
14448352Scael.Sh ERRORS
14548352Scael.Bl -tag -width [EINVAL]
14648352Scael.It Bq Er EINVAL
14748352ScaelThe
14848352Scael.Fa mode
14948352Scaelprovided to
15048352Scael.Fn fopen ,
15148352Scael.Fn fdopen ,
15246078Sbosticor
15348352Scael.Fn freopen
15446078Sbosticwas invalid.
15548352Scael.El
15648352Scael.Pp
15748352ScaelThe
15848352Scael.Fn fopen ,
15948352Scael.Fn fdopen
16047010Sbosticand
16148352Scael.Fn freopen
16248352Scaelfunctions
16347010Sbosticmay also fail and set
16448352Scael.Va errno
16547010Sbosticfor any of the errors specified for the routine
16648352Scael.Xr malloc 3 .
16748352Scael.Pp
16848352ScaelThe
16948352Scael.Fn fopen
17048352Scaelfunction
17146078Sbosticmay also fail and set
17248352Scael.Va errno
17346078Sbosticfor any of the errors specified for the routine
17448352Scael.Xr open 2 .
17548352Scael.Pp
17648352ScaelThe
17748352Scael.Fn fdopen
17848352Scaelfunction
17946078Sbosticmay also fail and set
18048352Scael.Va errno
18146078Sbosticfor any of the errors specified for the routine
18248352Scael.Xr fcntl 2 .
18348352Scael.Pp
18448352ScaelThe
18548352Scael.Fn freopen
18648352Scaelfunction
18746078Sbosticmay also fail and set
18848352Scael.Va errno
18946078Sbosticfor any of the errors specified for the routines
19048352Scael.Xr open 2 ,
19148352Scael.Xr fclose 3
19246078Sbosticand
19348352Scael.Xr fflush 3 .
19448352Scael.Sh SEE ALSO
19548352Scael.Xr open 2 ,
19648352Scael.Xr fclose 3 ,
19748352Scael.Xr fseek 3 ,
19848352Scael.Xr funopen 3
19948352Scael.Sh STANDARDS
20048352ScaelThe
20148352Scael.Fn fopen
20246078Sbosticand
20348352Scael.Fn freopen
20448352Scaelfunctions
20548352Scaelconform to
20648352Scael.St -ansiC .
20748352ScaelThe
20848352Scael.Fn fdopen
20948352Scaelfunction
21048352Scaelconforms to
21148352Scael.St -p1003.1-88 .
212