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?
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"
.