tags:

views:

83

answers:

3

One of the great joys of windows programming is remembering to put 'wb' or 'rb' or ios::binary in all the file open calls so that Windows doesn't merrily convert all your 0x13s.

I just had to convert a bunch of nicely standard code to use Createfile() to get a certain flag - and it occurred to me that there is no way to specify binary.
How does it know? I really don't want it changing bytes in my MP4 stream thank-you-very-much.

+1  A: 

You can't specify a binary or text flag, because to the Windows OS, all files are binary! The wb and rb options are introduced as a nicety, as part of the C IO stream functionality, and even then only on DOS / Windows, to help the developer read and write text files and perform CR/LF to LF conversion.

Michael Goldshteyn
+5  A: 

Because CreateFile doesn't do "text-mode"/newline conversions. Those are handled at a higher level, either in FILE for the CRT or iostreams for C++.

Roger Pate
Never really thought of Createfile as a lower level FILE, just as a windows only equivalent. I suppose to MS everything is Windows so you write cr/lf and read cr/lf, no need to ever convert.
Martin Beckett
@Martin Beckett: actually, the `CreateFile()` function is just poorly named. It is used to open many, many other types of resources (folders, drives, serial/parallel ports, etc.), for most of which text/binary doesn't make sense because you're not transferring text.
André Caron
CreateFile is the user-mode interface to the ZwCreateFile kernel function, so it's as low-level as you can get in user-mode.
Gerald
Actually I misspoke there... NtCreateFile is the user-mode interface to ZwCreateFile, and CreateFile calls NtCreateFile. So you could go a little lower than CreateFile by calling NtCreateFile directly, but it's somewhat uglier.
Gerald
+3  A: 

Everything is binary as far as the Windows API is concerned. Personally I prefer it that way. I never use the "text mode" in the standard library stuff.

Gerald
+1: I totally agree. Especially that many higher-level tools filter CR/LF only when they know you're reading text. For instance, the `<iostream>` standard library filters then out when using `operator>>` with `std::string` but not when using the `get(char*,streamsize)`.
André Caron