I've been trying to figure out the best way to write binary data to stdout from a C program. It works fine on Linux, but I'm having issues when I compile on Windows because "\n" gets converted to "\r\n".
Is there a standard way to write to stdout in some sort of binary mode which avoids newline conversion? If not, what is the simplest way to get Windows to stop doing this?
I'm using GCC and MinGW.
You can do something like that (which is sort of cross platform):
Or you can use
write()
andread()
system calls directly withfileno(stdin)
andfileno(stdout)
. Those system calls operate on lower level and don't do any conversions. But they also don't have buffering that you get fromFILE
streams.You can use
setmode(fileno(stdout), O_BINARY)
Wrap it in an ifdef if you want to keep it compatible with Linux.
See also http://msdn.microsoft.com/en-us/library/tw4k6df8(v=vs.80).aspx