
Hi. I could use an example of how to handle files (FILE *) with c2hs, but could not find any. 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? - J.W.

Hi Johannes,
The following might give you some ideas:
module Main where
import Control.Monad
import Foreign
import Foreign.ForeignPtr
import Foreign.C.String
import Foreign.C.Types
#include
Hi.
I could use an example of how to handle files (FILE *) with c2hs, but could not find any.
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?
- J.W.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ian Ross Tel: +43(0)6804451378 ian@skybluetrades.net www.skybluetrades.net

Quoth Johannes Waldmann
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

On Wed, Dec 17, 2014 at 10:44 AM, Donn Cave
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!)
About the only one is the underlying OS file descriptor; GHC's runtime implements its own native file handles, it does not wrap C stdio, and you cannot round-trip between a stdio (FILE *) and a GHC Handle. Using the file descriptor should work on both POSIX and Windows, as long as it's an ordinary file (that is, not a device, fifo, socket, directory (netbsd/freebsd), mailbox, etc.). You will need to use fdToHandle / handleToFd on the Haskell side and fdopen()/fileno() on the C side; the Haskell ones will ensure the buffers are properly dealt with, the C ones you must make sure to do so yourself. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
participants (4)
-
Brandon Allbery
-
Donn Cave
-
Ian Ross
-
Johannes Waldmann