[Haskell-cafe] How can I redirect stdout?

Matthew Bromberg mattcbro at earthlink.net
Fri Sep 29 14:44:58 EDT 2006

I'm reposting this in it's own new thread since I think it involves a 
general issue beyond exporting Haskell DLLs.

I am having some problems with GHCs stdout when a Haskell program is 
called from a windows program.

As I noted earlier I am calling some Haskell code from C as a bridge to 
being able to
ultimately call Haskell from Matlab 6.5.

The Haskell code is compiled into a .DLL file on a windows machine.
Matlab calls some C code which then calls the Haskell code.

As soon as it gets into the Haskell code I get this run time error in 

<stdout>: hPutChars: invalid argument (Bad File Descriptor)

The Haskell code seems to work correctly if called from a stand-alone C 
program. Moreover, if I scrub all print statements from the Haskell 
code, it appears to run correctly from Matlab.

Matlab does not properly redirect stdout so any printf calls from the C 
code simply fail to print.
However Haskell's behavior is to halt after generating a runtime error.

The C code used to generate the Haskell .dll is of course mingw gcc, but 
the C code used to create my Matlab mex file is Microsoft VC++ 6.0.  I 
tried to redirect stdout using freopen() in C, but that never seems to 
work with Microsoft.  At any rate it certainly doesn't effect Haskells 
use of stdout.  I think in general for windows programs there is no 
stdout defined, whereas it's always defined for console programs.

My question is, is there some way I can redirect stdout from inside 
Haskell so that all output is sent to a file?  Is there an equivalent of 
freopen() in Haskell that works?

More information about the Haskell-Cafe mailing list