xref: /original-bsd/lib/libc/stdio/fopen.3 (revision c43ebe10)
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) 03/05/91

FOPEN 3 ""
C 7
NAME
fopen, fdopen, freopen - open a stream
SYNOPSIS
#include <stdio.h>

FILE *
fopen(char *path, char *type);

FILE *
fdopen(int fildes, char *type);

FILE *
freopen(char *path, char *type, FILE *stream);
DESCRIPTION
Fopen opens the file named by path and associates a stream with it, returning a pointer for identifying the stream in subsequent operations.

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.

"SEE ALSO"
open(2), fclose(3), fseek(3), funopen(3)
"RETURN VALUES"
Upon successful completion fopen , fdopen and freopen return a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error.
ERRORS

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).

STANDARDS
Fopen and freopen conform to ANSI X3.159-1989 (``ANSI C''). Fdopen conforms to IEEE Std 1003.1-1988 (``POSIX'').
BUGS
Fdopen may not be portable to systems other than UNIX.