The solution (provided in Microsoft's documentation) is to add: #include <cstdio> #include <io.h> #include <fcntl.h>
and run:
int result = _setmode( _fileno(stdin), _O_BINARY );
if( result == -1 )
{ std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1;
}
result = _setmode( _fileno(stdout), _O_BINARY );
if( result == -1 )
{ std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1;
}
before using cin or cout. I'm not sure where to add this however, since
it needs to be called before any reads or writes are done, but only
needs to be called once. Any suggestions? At the moment in my own code,
I'm adding them to the tools source files, since that's where it's
determined that cin/cout can be used for input/output.
The solution (provided in Microsoft's documentation) is to add: #include <cstdio> #include <io.h> #include <fcntl.h>
and run:
int result = _setmode( _fileno(stdin), _O_BINARY );
if( result == -1 )
{ std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1;
}
result = _setmode( _fileno(stdout), _O_BINARY );
if( result == -1 )
{ std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1;
}
before using cin or cout. I'm not sure where to add this however, since
it needs to be called before any reads or writes are done, but only
needs to be called once. Any suggestions? At the moment in my own code,
I'm adding them to the tools source files, since that's where it's
determined that cin/cout can be used for input/output.
Reid is the guru here, but I'll inject my opinion. I think that this should be a method somewhere in the System library. Given that, you should change llvm-as.cpp (and all other tools with similar issues) from:
} else { // Specified stdout
// FIXME: cout is not binary!
Out = &std::cout;
}
... where "ChangeStandardStreamToBinary" is something that Reid likes.
The implementation of ChangeStandardStreamToBinary would do the code above in the win32 case, and would be a noop in the unix case. This is just one way to do it, I defer to Reid for what the "right" way to do it is
The approach to setting binary mode varies from platform to platform.
Thanks for providing the Win32 version. We will also need to work up a
Unix version (with variants).
The correct way to do this is to add a function to
include/llvm/System/Program.h and then implement that function variantly
for the different platforms. The function can then be called from the
main function of whatever program wants its stdin/stdout to be binary.
We can do this. Can you create a bug report with this code snippet in
it so we can keep track of it? That way, I won't forget that this needs
to be done.