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

Ian Ross ian at skybluetrades.net
Wed Dec 17 10:00:21 UTC 2014


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 <stdio.h>

{#pointer *FILE as File foreign finalizer fclose newtype#}

{#fun fopen as ^ {`String', `String'} -> `File'#}
{#fun fgetc as ^ {`File'} -> `Int'#}

main :: IO ()
main = do
  fp <- fopen "tst.dat" "r"
  c <- fgetc fp
  print c

(The "foreign finalizer" thing requires a recent C2HS.)

Cheers,

Ian.


On 17 December 2014 at 09:19, Johannes Waldmann <
waldmann at imn.htwk-leipzig.de> wrote:
>
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


-- 
Ian Ross   Tel: +43(0)6804451378   ian at skybluetrades.net
www.skybluetrades.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141217/dac3e826/attachment.html>


More information about the Haskell-Cafe mailing list