Name
fopen - Opens a file.
Syntax
#include <stdio.h>
FILE *fopen(path, type)
const char *path;
const char *type;
Description
The fopen function opens the file specified by path. The
character string type specifies the type of access requested
for the file, as follows:
Type Description
"r" Opens for reading. If "r" is the first
character in type, and the file does not
exist or cannot be found, the fopen call
will fail.
"w" Opens an empty file for writing. If the
given file exists, its contents are
destroyed.
"a" Opens for writing at the end of the file
(appending); creates the file first if it
doesn't exist.
"r+" Opens for both reading and writing. (The
file must exist.)
"w+" Opens an empty file for both reading and
writing. If the given file exists, its
contents are destroyed.
"a+" Opens for reading and appending; creates
the file first if it doesn't exist.
Use the "w" and "w+" types with care, as they can destroy
existing files.
Notes
When a file is opened with the "a" or "a+" type, all write
operations occur at the end of the file. Although the file
pointer can be repositioned using fseek or rewind, the file
pointer is always moved back to the end of the file before
any write operation is carried out. Thus, existing data
cannot be overwritten.
When the "r+", "w+", or "a+" type is specified, both reading
and writing are allowed (the file is said to be open for
``update''). However, when switching between reading and
writing, there must be an intervening fsetpos, fseek, or
rewind operation. The current position can be specified for
the fsetpos or fseek operation, if desired.
In addition to the values listed above, one of the following
characters can be appended to type or inserted before the +
character to specify the translation mode for new lines. For
example, r+b is the same as rb+.
Mode Meaning
t Open in text (translated) mode. In this
mode, carriage-return-line-feed (CR-LF)
combinations are translated into single
line feeds (LF) on input and LF
characters are translated to CR-LF
combinations on output. Also, CTRL+Z is
interpreted as an end-of-file character
on input. In files opened for reading or
reading/writing, fopen checks for a
CTRL+Z at the end of the file and removes
it, if possible. This is done because
using the fseek and ftell functions to
move within a file that ends with a
CTRL+Z may cause fseek to behave
improperly near the end of the file.
b Open in binary (untranslated) mode; the
above translations are suppressed.
The t option is not part of the ANSI standard for open, but
is a Microsoft extension and should not be used where ANSI
portability is desired.
If t or b is not given in type, the translation mode is
defined by the default-mode variable _fmode.
Return Value
The fopen function returns a pointer to the open file. A
null pointer value indicates an error.
See Also
fclose(DOS), fcloseall(DOS), fdopen(S), ferror(S),
fileno(S), freopen(DOS), open(DOS), setmode(DOS)
Example
#include <stdio.h>
FILE *stream;
main()
{
/* Attempt to open the file: */
if ((stream = fopen("data","r")) == NULL)
printf("Could not open file\n");
else
printf( "File opened for reading\n" );
}
This program uses fopen to open a file named data for
input.
(printed 6/18/89)