fopen, freopen, fdopen - open a stream


#include "stdio_p.h"

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

FILE *freopen (path, type, stream)
char *path, *type;
FILE *stream;

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


fopen opens the file named by path and associates a stream with it. fopen returns a pointer to be used to identify the stream in subsequent operations.

type is a character string having one of the following values:

"r"	open for reading
"w"	create for writing
"a"	append; open for writing at end of file, or create for writing
"r+"	open for update (reading and writing)
"w+"	create for update
"a+"	append; open or create for update at end of file

freopen substitutes the named file in place of the open stream. It returns the original value of stream. The original stream is closed, regardless of whether the open ultimately succeeds.

freopen is typically used to attach the preopened constant names stdin, stdout, and stderr to specified files. fdopen associates a stream with a file descriptor obtained from open. The type of the stream must agree with the mode of the open file.

When a file is opened for update, both input and output may be done on the resulting stream. However, output may not be directly followed by input without an intervening fseek or rewind, and input may not be directly followed by output without an intervening fseek or rewind, or an input operation which encounters end of file.


open(2F) , fclose(3P).


fopen and freopen return the pointer NULL if path cannot be accessed.