Why doesn't fwrite() write a Binary File using “wb”, in C, on Mac OS X?

后端 未结 6 1410
鱼传尺愫
鱼传尺愫 2020-12-20 21:15

For the sake of learning C and understanding the difference between Binary Files and Text Files, I\'m attempting to write a string to file as both file types like so:

<
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 21:32

    All files are binary.

    The difference between "text files" and "binary files" in the context of fopen is that the standard library may use a filter for reading and writing the file when in text mode. In binary mode, what you write is always what is written.

    The only practical example I know of is in Windows, where opening a file in text mode will make it convert between Windows-style line endings (CRLF, "\r\n") and C-style/Unix-style line-endings (LF, "\n"). For example when you open a file for reading in Windows in text mode, line endings will appear as "\n" in your program. If you were to open it in binary mode, line endings would appear as "\r\n" in your program.


    You can also inspect your file with cat, for example:

    cat filename

    You should get the output: I am a string!

提交回复
热议问题