.TH FOPEN 3S .UC .SH NAME fopen, freopen, fdopen \- open a stream .SH SYNOPSIS .B #include .PP .SM .B FILE .B *fopen(filename, type) .br .B char *filename, *type; .PP .SM .B FILE .B *freopen(filename, type, stream) .br .B char *filename, *type; .br .SM .B FILE .B *stream; .PP .SM .B FILE .B *fdopen(fildes, type) .br .B char *type; .SH DESCRIPTION .I Fopen opens the file named by .I filename and associates a stream with it. .I Fopen returns a pointer to be used to identify the stream in subsequent operations. .PP .I Type is a character string having one of the following values: .TP 5 "r" open for reading .ns .TP 5 "w" create for writing .ns .TP 5 "a" append: open for writing at end of file, or create for writing .PP In addition, each .I type may be followed by a '+' to have the file opened for reading and writing. "r+" positions the stream at the beginning of the file, "w+" creates or truncates it, and "a+" positions it at the end. Both reads and writes may be used on read/write streams, with the limitation that an .I fseek, rewind, or reading an end-of-file must be used between a read and a write or vice-versa. .PP .I Freopen substitutes the named file in place of the open .IR stream . It returns the original value of .IR stream . The original stream is closed. .PP .I Freopen is typically used to attach the preopened constant names, .B stdin, stdout, stderr, to specified files. .PP .I Fdopen associates a stream with a file descriptor obtained from .I open, dup, creat, or .IR pipe (2). The .I type of the stream must agree with the mode of the open file. .SH "SEE ALSO" open(2), fclose(3S) .SH DIAGNOSTICS .I Fopen and .I freopen return the pointer .SM .B NULL if .I filename cannot be accessed. .SH BUGS .I Fdopen is not portable to systems other than UNIX. .PP The read/write .IR type s do not exist on all systems. Those systems without read/write modes will probably treat the .I type as if the '+' was not present.