.\" Copyright (c) 1990 The Regents of the University of California. .\" All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" Chris Torek. .\" .\" %sccs.include.redist.man% .\" .\" @(#)fopen.3 6.6 (Berkeley) %G% .\" .TH FOPEN 3 "" .UC 7 .SH NAME fopen, fdopen, freopen \- open a stream .SH SYNOPSIS .nf .ft B #include FILE * fopen(char *path, char *type); FILE * fdopen(int fildes, char *type); FILE * freopen(char *path, char *type, FILE *stream); .ft R .fi .SH DESCRIPTION .I Fopen opens the file named by .I path and associates a stream with it, returning a pointer for identifying the stream in subsequent operations. .PP .I Type is one of the following character strings: .TP ``r'' Open for reading. The stream is positioned at the beginning of the file. .TP ``r+'' Open for reading and writing. The stream is positioned at the beginning of the file. .TP ``w'' Open for writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. .TP ``w+'' Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. .TP ``a'' Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. .TP ``a+'' Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. .PP The .I type string can also include the letter ``b'' either as a third character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with ANSI X3.159-1989 (``ANSI C'') and has no effect; the ``b'' is ignored. .PP Any created files will have mode ``S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH'' (0666), as modified by the process' umask value (see .IR umask (2)). .PP Reads and writes may be intermixed on read/write streams in any order, and do not require an intermediate seek as in previous versions of stdio. This is not portable to other systems, however; ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. .PP .I Fdopen associates a stream with a file descriptor obtained from .IR creat (2), .IR dup (2), .IR open (2), or .IR pipe (2). The .I type of the stream must be compatible with the mode of the file descriptor. .PP .I Freopen substitutes the named file in place of the open .IR stream and returns a pointer to it. The original stream is closed. .I Freopen is typically used to attach the preopened constant names, .IR stdin , .IR stdout , and .I stderr to files. .SH "SEE ALSO" open(2), fclose(3), fseek(3), funopen(3) .SH "RETURN VALUES" Upon successful completion .IR fopen , .I fdopen and .I freopen return a FILE pointer. Otherwise, NULL is returned and the global variable .I errno is set to indicate the error. .SH ERRORS .TP 15 [EINVAL] The .I type provided to .IR fopen , .IR fdopen , or .IR freopen was invalid. .PP .IR Fopen , .I fdopen and .I freopen may also fail and set .IR errno for any of the errors specified for the routine .IR malloc (3). .PP .I Fopen may also fail and set .IR errno for any of the errors specified for the routine .IR open (2). .PP .I Fdopen may also fail and set .IR errno for any of the errors specified for the routine .IR fcntl (2). .PP .I Freopen may also fail and set .IR errno for any of the errors specified for the routines .IR open (2), .IR fclose (3) and .IR fflush (3). .SH STANDARDS .I Fopen and .I freopen conform to ANSI X3.159-1989 (``ANSI C''). .I Fdopen conforms to IEEE Std 1003.1-1988 (``POSIX''). .SH BUGS .I Fdopen may not be portable to systems other than UNIX.