I was reading a question and one of the comments mentioned that "The binary flag is only relevant on Windows". Given the context of the question and answer, I interpret this statement as on non-windows machines, we don't need to specify text or binary mode when doing file IO

The context of the question is to read all bytes of a file into a string (presumably preserving bytes). It seems very relevant to me whether I'm reading the data as text or bytes, since reading text involves dealing with encoding. If I'm reading a UTF-16 file, for example, what I get in text mode is much different from what I get in binary mode.

Assuming my interpretation of the statement is correct, is it unnecessary to specify whether a file should be opened in text or binary mode on non-windows machines? That is, all files are read in binary mode by default.

Otherwise, what could the statement be referring to? (the comment received a number of upvotes so presumably this is not just one person's opinion)

  • Daniel (the person posting the comment) is an active user; he was on the site 3 hours ago. Why don't you post a comment asking for clarification? May 13, 2014 at 21:05
  • 1
    This is a reasonable question, though, assuming it isn't a duplicate.
    – user53141
    May 13, 2014 at 21:09

1 Answer 1


The "binary" flag exists for the fopen() function call in the C standard library. On my nearest Linux system, the documentation states:

The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the 'b' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-Unix environments.)

The C standard library functions do not deal with text encodings. Data read from or written to files are bytes. On some systems such as Windows, the line-endings are conventionally \r\n but on POSIX systems, the line-endings are just \n. This is one of the things that the "binary" flag controls (another thing may be the details of end-of-file handling).

However, in Ruby, the IO::new function treats the "b" flag as follows (emphasis mine):

"b" Binary file mode
Suppresses EOL <-> CRLF conversion on Windows. And sets external encoding to ASCII-8BIT unless explicitly specified.

This means that additionally, Ruby sets the character encoding depending on whether the "b" flag is included. This means that its use is not specific to Windows, and can affect the behaviour of your program even on a POSIX system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.