[LLVMdev] Binary output to cout on Windows

Reid Spencer reid at x10sys.com
Tue May 23 11:25:15 PDT 2006

Hi Michael,

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. 



On Tue, 2006-05-23 at 10:27 -0700, Michael Smith wrote:
> 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.
> ~Michael
> -----Original Message-----
> From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu]
> On Behalf Of Chris Lattner
> Sent: Monday, May 22, 2006 4:36 PM
> To: LLVM Developers Mailing List
> Subject: RE: [LLVMdev] Binary output to cout on Windows
> On Mon, 22 May 2006, Michael Smith wrote:
> > llvm-as interprets "-" as using standard output (cout), so llvm-as <
> > input.ll -o - | opt has the same behavior. You'll actually find a
> > comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope
> that
> > there's a good way to do it.
> Please see:
> http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13
> We'd welcome patches to do this, and we can localize this in our system 
> support library, but you'd have to figure out the magic to make this 
> happen on win32.
> -Chris
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20060523/09c7eba0/attachment.sig>

More information about the llvm-dev mailing list