All rights reserved.
This code is derived from software contributed to Berkeley by
Chris Torek.
%sccs.include.redist.man%
@(#)fopen.3 6.6 (Berkeley) 03/05/91
#include <stdio.h> FILE * fopen(char *path, char *type); FILE * fdopen(int fildes, char *type); FILE * freopen(char *path, char *type, FILE *stream);
Type is one of the following character strings:
``r'' Open for reading. The stream is positioned at the beginning of the file.
``r+'' Open for reading and writing. The stream is positioned at the beginning of the file.
``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.
``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.
``a'' Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file.
``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.
The 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.
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 umask (2)).
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.
Fdopen associates a stream with a file descriptor obtained from creat (2), dup (2), open (2), or pipe (2). The type of the stream must be compatible with the mode of the file descriptor.
Freopen substitutes the named file in place of the open stream and returns a pointer to it. The original stream is closed. Freopen is typically used to attach the preopened constant names, stdin , stdout , and stderr to files.
15 [EINVAL] The type provided to fopen , fdopen , or freopen was invalid.
Fopen , fdopen and freopen may also fail and set errno for any of the errors specified for the routine malloc (3).
Fopen may also fail and set errno for any of the errors specified for the routine open (2).
Fdopen may also fail and set errno for any of the errors specified for the routine fcntl (2).
Freopen may also fail and set errno for any of the errors specified for the routines open (2), fclose (3) and fflush (3).