Fopen open a file and return a file pointer #include <stdio.h> FILE *fopen char *filename char *action FILE *freopen char *filename char *action FILE *streak FILE *fdopen FILE *filedes char *action Description 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: r open for reading w create for writing a append(write) at end of file, or create for writing r+ open for update w+ create for update a+ create or open for update at end of file d directory 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: stdin the standard input - equivalent to path number 0 stdout the standard output - equivalent to path number 1 stderr the 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()