3 Dec 15:09
handles and foreign C functions
From: Andrea Rossato <mailing_list <at> istitutocolli.org>
Subject: handles and foreign C functions
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-12-03 14:09:21 GMT
Subject: handles and foreign C functions
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-12-03 14:09:21 GMT
Hello,
suppose a simple C function like this:
void printStuff(FILE *f)
{
fprintf (f, "Hello World\n");
}
In order to use it I need to import fopen and pass to it a Ptr CFile:
foreign import ccall unsafe "fopen" fopen :: CString -> CString -> IO (Ptr CFile)
foreign import ccall unsafe "fclose" fclose :: Ptr CFile -> IO CInt
foreign import ccall unsafe "printStuff" printStuff :: Ptr CFile -> IO ()
main =
withCString "tmp.txt" $ \cpath ->
withCString "w" $ \cmode -> do
cfile <- throwErrnoIfNull "fopen: " (fopen cpath cmode)
printStuff cfile
fclose cfile
How can I pass to printStuff a stdout FILE pointer?
If, for instance, I do:
foreign import ccall unsafe "stdout" c_stdout :: Ptr CFile
main =
withCString "tmp.txt" $ \cpath ->
(Continue reading)
RSS Feed