Name

fopen function — Opens a file

Synopsis

FILE* fopen(const char* filename, const char* mode)

The fopen function opens a file.

image with no caption

The filename parameter specifies the filename in an implementation-defined manner. The mode parameter specifies how to open the file. The mode must begin with one of the strings listed in Table 13-4. Additional characters can follow, and the interpretation of the extra characters is implementation-defined. Mode strings are case-sensitive.

Table 13-4. File open modes

Mode string

Description

a

Append: opens an existing file for appending, that is, every write is forced to the end of the file. If the file to be opened does not exist, a creates it.

r

Read: opens an existing file for reading.

w

Write: creates a new file for writing. If the file already exists, w truncates it to zero length.

ab

Append in binary mode.

rb

Read in binary mode.

wb

Write in binary mode.

a+

Append update: opens a file in append mode and allows it to be read.

r+

Read update: opens an existing file for reading, and also allows writing.

w+

Write update: creates a new file for writing, and also allows reading. If the file already exists, w+ truncates it to zero length.

ab+ or a+b

Append update in binary mode.

rb+ or r+b

Read update in binary mode.

wb+ or w+b

Write update in binary mode.

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.