Fopenopen a file and return a file pointer
#include <stdio.h>
FILE *fopenchar *filenamechar *actionFILE *freopenchar *filenamechar *actionFILE *streakFILE *fdopenFILE *filedeschar *actionDescription
Fopen returns a pointer to a file structure (file pointer) if
the file name in the string pointed to by "filename" can be
validly opened with the action in the string pointed to by
"action".
The valid actions are:
ropen for readingwcreate for writingaappend(write) at end of file, or create for writingr+open for updatew+create for updatea+create or open for update at end of fileddirectory read
Any action may have an x after the initial letter which
indicates to fopen() that it should look in the current
execution directory if a full path is not given, and
the x also specifies that the file should have execute permission.
E.g. f = fopen(fred,wx);
Opening for write will perform a creat(). If a file with the
same name exists when the file is opened for write, it will be truncated
to zero length. Append means open for write and
position to the end of the file. Writes to the file via
putc() etc. will extend the file. Only if the file does not
already exist will it be created.
NOTE that the type of a file structure is pre-defined in
stdio.h as FILE, so that a user program may decale or define
a file pointer by, for example, FILE *f;
Three file pointers are available and can be considered open
the moment the program runs:
stdinthe standard input - equivalent to path number 0stdoutthe standard output - equivalent to path number 1stderrthe standard error output - equivalent to path number 2
All files are automatically buffered except stderr, unless a
file is made unbuffered by a call to setbuf() (q.v.).
Freopen is usually used to attach stdin, stdout, and stderr to
specified files. Freopen substitutes the file passed to it
instead of the open stream. The original stream is closed.
NOTE that the original stream will be closed even if the open
does not succeed.
Fdopen associates a stream with a file descriptor. The streams
type(r,w,a) must be the same as the mode of the open file.
Caveats
The action passed as an argument to fopen must be a pointer
to a string, not a character. For example
fp = fopen(fred,r); is correct but
fp = fopen(fred,'r'); is not.
Diagnostics
Fopen returns NULL (0) if the call was unsuccessful.
See Also
System call
open(),
fclose()