[Haskell-cafe] Writing binary files?

Glynn Clements glynn.clements at virgin.net
Sun Sep 12 10:20:09 EDT 2004


Abraham Egnor wrote:

> Passing a Ptr isn't that onerous; it's easy enough to make functions
> that have the signature you'd like:
> 
> import System.IO
> import Data.Word (Word8)
> import Foreign.Marshal.Array
> 
> hPutBytes :: Handle -> [Word8] -> IO ()
> hPutBytes h ws = withArray ws $ \p -> hPutBuf h p $ length ws
> 
> hGetBytes :: Handle -> Int -> IO [Word8]
> hGetBytes h c = allocaArray c $ \p ->
>     do c' <- hGetBuf h p c
>        peekArray c' p

The problem with this approach is that the entire array has to be held
in memory, which could be an issue if the amount of data involved is
large.

-- 
Glynn Clements <glynn.clements at virgin.net>


More information about the Haskell-Cafe mailing list