Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The C Standard identifies specific strings to use for the mode on calls to fopen().  C11  provides a new mode flag 'x' that provides the mechanism needed to determine if the file that is to be opened exists. To be strictly conforming and portable, one of the strings from the following table (adapted from the C Standard) must be used:

Strings to Use for the mode Mode on Calls to fopen()

mode String

Result

r

Open text file for reading

w

Truncate to zero length or create text file for writing

wx

Create text file for writing

a

Append; open or create text file for writing at end-of-file

rb

Open binary file for reading

wb

Truncate to zero length or create binary file for writing

wbx

Create binary file for writing

ab

Append; open or create binary file for writing at end-of-file

r+

Open text file for update (reading and writing)

w+

Truncate to zero length or create text file for update

w+x

Create text file for update

a+

Append; open or create text file for update, writing at end-of-file

r+b or rb+

Open binary file for update (reading and writing)

w+b or wb+

Truncate to zero length or create binary file for update

w+bx or wb+x

Create binary file for update

a+b or ab+

Append; open or create binary file for update, writing at end-of-file

...

[ISO/IEC 9899:2011]Section 7.21.5.3, "The fopen function"

 

...