.TH FOPEN 3S .CT 2 file_io .SH NAME fopen, freopen, fdopen, fclose, fflush \(mi open, close, or flush a stream .SH SYNOPSIS .nf .B #include <stdio.h> .PP .B FILE *fopen(filename, type) .B char *filename, *type; .PP .B FILE *freopen(filename, type, stream) .B char *filename, *type; .B FILE *stream; .PP .B FILE *fdopen(fildes, type) .B char *type; .PP .B int fclose(stream) .B FILE *stream; .PP .B int fflush(stream) .B FILE *stream; .fi .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: .nf .ta 8n \fL"r"\fP open for reading \fL"w"\fP create for writing \fL"r+w"\fP \fL"w+r"\fP open for reading and writing \fL"a"\fP append: open for writing at end of file, or create for writing .fi .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. .I Freopen is typically used to attach the preopened constant names .LR stdin , .L stdout and .L stderr to specified files. .PP .I Fdopen associates a stream with a file descriptor. The .I type of the stream must agree with the mode of the open file. .PP .I Fclose causes any buffers for the named .I stream to be emptied, and the file to be closed. Buffers allocated by the standard input/output system are freed. .PP .I Fclose is performed automatically upon calling .IR exit (3). .PP .I Fflush causes any buffered data for the named output .I stream to be written to that file. The stream remains open. .SH "SEE ALSO" .IR open (2), .IR popen (3), .IR stdio (3), .IR ferror (3) .SH DIAGNOSTICS .I Fopen and .I freopen return .B NULL if .I filename cannot be accessed. .PP .I Fclose and .I fflush return .B EOF if .I stream is not associated with a file, or if buffered data cannot be transferred to that file.