Force C to readin stdin as binary using MINGW

1.5k Views Asked by At

So, I have a use case where I need to force a C program cross compiled to Windows to read in stdin as a binary format. This works great for Windows compilers, but unfortunately I am compiling this program using MinGW so I can't get access to setting the mode of the stdin as this is a pre-MSDOS, preUNIX convention. How would I force this to occur while still compiling this code in GCC? Any ideas?

The statements at fault are the following:

__setmode(__fileno(stdin), O_BINARY);
__setmode(__fileno(stdout), O_BINARY);
1

There are 1 best solutions below

4
On BEST ANSWER

The statements above are not related to the compiler but to the header files and C library you are linking to.

MinGW uses the Microsoft runtime library and compatible header files. You probably have an incorrect spelling for these functions, try removing some of the _, grep for setmode in the header files, etc:

grep -r setmode /usr/include
grep -r O_BINARY /usr/include

If you cannot find these, copy the prototypes and #define from the microsoft headers.

cremno suggested this:

_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);

If you really cannot find these symbols, try this:

extern int _setmode(int, int);
_setmode(0, 0x8000);
_setmode(1, 0x8000);