[Haskell-cafe] how to handle FILE* with c2hs

Donn Cave donn at avvanta.com
Wed Dec 17 15:44:14 UTC 2014


Quoth Johannes Waldmann <waldmann at imn.htwk-leipzig.de>,

> I want to call some "void dump (..., FILE * f)" function.
> I want to use this for a handle that I obtained in Haskell land.
>
> Sure I can rewrite/add the C code so that it takes a filepath (string)
> and opens the file by itself, but should I?

You could move that logic into Haskell, as illustrated in a previous
follow-up, but I can't think of any direct interface between C FILE
and Haskell Handle, and I guess that because of the essential internal
nature of the two, that's to be expected.  (Or maybe I just don't
know where to look!)

In a POSIX situation you could get the system file descriptor from
the Handle, and convert that to a FILE pointer, but at the cost
of closing the Handle.  Don't know if that would work so well on
non-POSIX platforms.  If the Haskell side will simply open the
file for this purpose, then I think you indeed might as well do
that in C (whether by calling fopen() from Haskell or writing a
C wrapper that takes a file path name.)

	Donn


More information about the Haskell-Cafe mailing list