I already read the C++ Reference about fopen access modes, but I don't understand the difference between "a+b"
and "ab+"
, or between "w+b"
and "wb+"
.
Asked
Active
Viewed 1,434 times
-1

Lundin
- 195,001
- 40
- 254
- 396

m4tthew8872
- 3
- 2
-
6As stated in the link you provide, there is no difference : _This additional "b" character can **either be appended** at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") **or be inserted** between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+")._ – Jabberwocky Jun 07 '18 at 10:33
-
@Jabberwocky next time I’ll read more accurately, thanks :) – m4tthew8872 Jun 07 '18 at 10:55
-
Hmmm, Interesting to use a site called "C++ Reference" for details about C. – chux - Reinstate Monica Jun 07 '18 at 12:04
-
There is no site called “C Reference” – m4tthew8872 Jun 07 '18 at 12:13
3 Answers
2
The ordering of the mode flags is not relevant. So ab+
and a+b
are equivalent.
The b
flag opens the file in binary mode, which disables the special handling of certain characters in DOS and Windows, but has no effect on macOS and POSIX systems.

iBug
- 35,554
- 7
- 89
- 134
-
The only difference with a text file is the handling of some characters? – m4tthew8872 Jun 07 '18 at 10:50
-
"no effect on macOS" --> aside from the earlier Macs [differences between end of line characters](https://confluence.qps.nl/fledermaus/questions-answers/other/differences-in-end-of-line-characters-mac-windows-and-linux) – chux - Reinstate Monica Jun 07 '18 at 11:59
-
1
The ordering of b
and +
don’t matter. There is no difference. So a+b
and ab+
mean the same thing.

dbush
- 205,898
- 23
- 218
- 273
1
The two forms are equivalent.
From the C standard C11 7.21.5.2:
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
As for the b
itself it means that the file is used as a pure binary file, rather than as a text file.

Lundin
- 195,001
- 40
- 254
- 396