Opens a file.
#include <stdio.h>
FILE *fopen( const char *filename, const char *mode );
filename | Path name of file | |
mode | Type of access permitted |
The fopen function opens the file specified by filename. The character string mode specifies the type of access requested for the file, as follows:
Type | Description |
"r" | Opens for reading. If 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. |
When a file is opened with the "a" or "a+" access 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+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch 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, the following characters can be included in mode to specify the translation mode for newline characters:
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 for 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. |
c | Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called. |
n | Reset the commit flag for the associated filename to “no-commit”. This is the default. It will also override the global commit flag if you have linked your program with COMMODE.OBJ. The global commit flag default is “no-commit” unless you explicitly link your program with COMMODE.OBJ. |
If t or b is not given in mode, the translation mode is defined by the default-mode variable _fmode. If t or b is prefixed to the argument, the function will fail and return NULL.
For a discussion of text and binary modes see “Input and Output”.
The fopen function returns a pointer to the open file. A null pointer value indicates an error.
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
Note that the c, n, and t options are not part of the ANSI standard for fopen; they are Microsoft extensions and should not be used where ANSI portability is desired.
fclose, _fcloseall, _fdopen, ferror, _fileno, freopen, _open, _setmode
/* FOPEN.C: This program opens files named “data” and “data2". It uses
* fclose to _close ”data" and _fcloseall to close all remaining files.
*/
#include <stdio.h>
FILE *stream, *stream2;
void main( void )
{
int numclosed;
/* Open for read (will fail if 'data' does not exist) */
if( (stream = fopen( “data”, “r” )) == NULL )
printf( “The file 'data' was not opened\n” );
else
printf( “The file 'data' was opened\n” );
/* Open for write */
if( (stream2 = fopen( “data2", ”w+" )) == NULL )
printf( “The file 'data2' was not opened\n” );
else
printf( “The file 'data2' was opened\n” );
/* Close stream */
if( fclose( stream ) )
printf( “The file 'data' was not closed\n” );
/* All other files are closed: */
numclosed = _fcloseall( );
printf( “Number of files closed by _fcloseall: %u\n”, numclosed );
}
The file 'data' was opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1