With the
mode specifiers above the file is open as a
text file. In order to open a file as a
binary file, a "b" character has to be included in the
mode string. 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+").
Additional characters may follow the sequence, although they should have no effect. For example,
"t" is sometimes appended to make explicit the file is a text file.
In the case of
text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format. In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability.
For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (
fflush) or repositioned (
fseek,
fsetpos,
rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.