How do I inform Windows that I'm writing a binary file?

(devblogs.microsoft.com)

14 points | by ingve 1 day ago

3 comments

  • Isamu 37 minutes ago
    Going all the way back to the earliest C compilers. There was a decision made to make “\n” just work on DOS for portability of Unix programs, and to make the examples from the C programming book just work.

    But in Unix “\n” is a single byte, and in DOS it is 2. So they introduced text and binary modes for files on DOS. Behind the scenes the library will handle the extra byte. This is not necessary in Unix.

    I used to have to be careful about importing files to DOS. Did the file come from Unix?

    • criddell 30 minutes ago
      Linefeed (\n) is a single byte in DOS as well.

      I think you are talking about carriage return linefeed pair (CRLF or \r\n),

      These control codes go back to line printers. Linefeed advances the paper one line and carriage return moves the print head to the left.

  • lmm 56 minutes ago
    The article seems to be taking the position that the C runtime library is not part of "Windows", which feels like a rather odd view to me. What is the stable API that Windows offers to application developers if not that?
    • ChrisSD 46 minutes ago
      The Win32 API. E.g. using WriteFile to write files (https://learn.microsoft.com/en-us/windows/win32/api/fileapi/...)

      It wasn't until fairly recently that the C runtime was stably shipped with Windows. Previously you had to install the correct version of the C library alongside your application.

      • lmm 35 minutes ago
        > The Win32 API. E.g. using WriteFile to write files (https://learn.microsoft.com/en-us/windows/win32/api/fileapi/...)

        Which is called from what, if not C? Does windows really offer no API for writing text (rather than bytes) to files? Or does it rely on the application developer to manage line endings in their own code? Neither of those sounds very developer-friendly.

        • ChrisSD 13 minutes ago
          Calling it from C does not mean you need a full C standard library to exist. For example, much of the C standard library is itself written in C. But it's a "freestanding" C which assumes only a minimal set of library functions exist (e.g. functions for copying memory from one place to another, filling memory with zeroes, etc).

          And you can of course use non-C languages to call the Win32 API. Or even directly using assembly code.

        • p_l 31 minutes ago
          The whole issue is specific to C and languages that copied C or use its runtime underneath in implementations (like Python)

          For reference, Unix has no API other than bytes either.

    • p_l 37 minutes ago
      Indeed, C runtime is not part of windows API, and it's normal to have a program include few different copies of C runtime library due to different modules compiled with different compilers/options.

      C runtime library being part of OS is accidental thing in Unix, 16bit and 32bit Windows API even does not use C-compatible ABI (instead, Pascal-compatible one is present)

  • saltyoldman 1 day ago
    fopen(..., "wb") ?
    • qbane 57 minutes ago
      It's C library taking care of the "b" part for you according to the article.
      • wahern 43 minutes ago
        It's the other way around. It's the C runtime that treats text ("t") mode differently, because the C standard specifies \n as a line delimiter but the Windows convention is \r\n. In text mode C stdio translates between \n and \r\n. In binary mode it does no translation.