C: How is binary-mode versus text-mode implemented for `fopen`?

86 Views Asked by At

As a learning exercise, I am attempting to write a platform layer in C for Windows and GNU/Linux operating systems.

I am currently interested in implementing a function which opens a file on the host platform in a provided mode, similar to fopen from <stdio.h>. I know there is the Windows API function CreateFile from <fileapi.h>, and the GNU function open from <fcntl.h>; I am thinking I can use these to open a file from disk for reading or writing. However, fopen also provides a mode choice between opening a file in binary-mode versus text-mode. I do not entirely understand how the difference between these works under the hood; how would I go about implementing this functionality in my version?

2

There are 2 best solutions below

0
chux - Reinstate Monica On BEST ANSWER

do not entirely understand how the difference between these works under the hood;

When code uses "b" to open a file, there is no translation. Whatever is in the file is what is read. Whatever is written to the file is written.

When code does not use "b" to open a file, there is a potential translation. An outgoing "\n" may get translated into "\r\n", "\r", "\n" or something else. The last write may append a Ctrl z or not. The beginning of the file may include a BOM. Other translations are possible. Reading of such files may consume the above mentioned and present the code with less info. There are many implementation specific aspects to reading and writing such a text file.

When reading/writing a text file, do not use "b". Otherwise do not open with a "b".


how would I go about implementing this functionality in my version?

If reading a text file, do not use "b", otherwise use "b".

2
Andreas Wenzel On

On Microsoft Windows, text files usually use the characters \r\n (carriage-return followed by line-feed) as line endings. When reading from files opened in text mode, the \r\n line endings get translated to \n, so that it appears to the application that the line endings consisted of \n instead of \r\n. Also, when writing to files in text mode, \n line endings get translated to \r\n. These translations do not happen in binary mode. Also, the byte value 0x1A is interpreted as the end of the file in text mode, but not in binary mode.

However, on GNU/Linux (and all other POSIX platforms), there is no difference between text mode and binary mode. In both modes, no translation takes place. This is because on that platform, the line endings of text files natively only consist of \n, so no translation is required.