fopen()

Opens a file as a stream.

  #include <stdio.h> 
  FILE *fopen(const char *filename, const char *mode);   
Parameter

filename

A pointer to a character string containing the name of the file to open.

mode

A pointer to a character string specifying the operations be performed.

Remarks

The fopen() function opens a file specified by filename, and associates a stream with it. The fopen() function returns a pointer to a FILE. This pointer is used to refer to the file when performing I/O operations.

The mode argument specifies how the file is to be used. Table 20.1 describes the values for mode.

A file opened with an update mode ("+") is buffered. The file cannot be written to and then read from unless the write operation and read operation are separated by an operation that flushes the stream's buffer. This can be done with the fflush() function or one of the file positioning operations ( fseek(), fsetpos(), or rewind()).

Similarly, a file cannot be read from and then written to without repositioning the file using one of the file positioning functions unless the last read or write reached the end-offile.

All file modes, except the append modes ( "a", "a+", "ab", "ab+") set the file position indicator to the beginning of the file. The append modes set the file position indicator to the end-of-file.

Note: All write modes, including update mode ( "w+") and write/read update mode ( "wb+"), delete data in a file when an existing file is opened.
The table below describes the values for mode.
Table 1. Modes for fopen()
Mode Description
"r" Specifies read mode. Opens an existing text file for reading only.
"w" Specifies write mode. Creates a new text file for writing, or opens then truncates an existing file. Writing starts at the beginning of the file.
"a" Specifies append mode. Creates a new text file for writing, or opens then truncates an existing file. Writing starts at the end-offile position.
"r+" Specifies update mode. Opens an existing text file for reading and writing.
"w+" Specifies update mode. Creates a new text file for reading and writing, or opens then truncates an existing file for reading and writing. The file position is at the beginning of the file.
"a+" Specifies update mode. Creates a new text file for reading and writing, or opens then truncates an existing file for reading and writing. The file position is at the end of file.
"rb" Specifies binary read mode. Opens an existing file for binary reading only.
"wb" Specifies binary write mode. Creates a new file for binary writing, or opens then truncates an existing file for binary writing. Writing starts at the beginning of the file.
"ab" Specifies binary append mode. Creates a new file for binary writing, or opens then truncates an existing file for binary writing. Writing starts at the end-of-file position.
"r+b"or "rb+" Specifies binary update mode. Opens an existing file for reading and writing binary data.
"w+b"or "wb+" Specifies binary update mode. Creates a new file for reading and writing binary data, or opens then truncates an existing file for reading and writing. The file position is at the beginning of the file.
"a+b"or "ab+" Specifies binary update mode. Creates a new file for reading and writing binary data, or opens then truncates an existing file for reading and writing. The file position is at the end of the file.

fopen() returns a pointer to a FILE if it successfully opens the specified file for the specified operation. fopen() returns a null pointer (NULL) when it is not successful.

Listing: Example of fopen() usage

#include <stdio.h> 
#include <stdlib.h> 
int main(void) 
{ 
FILE *f; 
int count; 
// create a new file for output 
if (( f = fopen("count.txt", "w")) == NULL) { 
printf("Can't create file.\n"); 
exit(1); 
} 
// output numbers 0 to 9 
for (count = 0; count < 10; count++) 
fprintf(f, "%5d", count); 
// close the file 
fclose(f); 
// open the file to append 
if (( f = fopen("count.txt", "a")) == NULL) { 
printf("Can't append to file.\n"); 
exit(1); 
} 
// output numbers 10 to 19 
for (; count <20; count++) 
fprintf(f, "%5d\n", count); 
// close file 
fclose(f); 
return 0; 
} 
Output to file count.txt: 
0 1 2 3 4 5 6 7 8 9 10 
11 
12 
13 
14 
15 
16 
17 
18 
19