Sollertis Sollertis - 6 months ago 26
C Question

creating .mid file: writing a '\n' causes '\r\n' in Windows

I have created a

file by writing bytes to a file and save it as
. I can run it and it works, but there are some special cases where it does not.

If I write a byte containing
) then it will instead write 2 bytes
, which makes the
not runnable. (This is normal for Windows machine to do, but not desirable in my case.) An example of writing
could be when picking the key which is being represented by

Is there a workaround to write
and not
or another way to make sure that byte written is ASCII
on a Windows machine?


Answer Source

On linux/unix, it doesn't matter whether you specify "wb" or "w" to create a file.

But creating a text file using fopen in windows means that all \n are converted to \r\n, so if you're using this to create binary files, the binary files will be "corrupt" if there are some bytes with value "10" (linefeed)

Simple solution: always use fopen("file.bin","wb") when creating a binary file, on all platforms so your code is portable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download