Thanks (was Re: storing to a file)

Johan Steunenberg steunenberg_ghc_user@steunenberg.de
18 Nov 2002 10:58:55 +0100


Thanks a lot.

after being offline for three days, it is great to see the amount of
answers. I'll have a try. And I realize even more that the world of
Haskell is pretty different from my C++/Java world. (Though it reminds
me of template meta programming)

Have a nice week,

Johan Steunenberg 
Am Fre, 2002-11-15 um 01.46 schrieb Glynn Clements:
> 
> Johan Steunenberg wrote:
> 
> > thanks for your advice, I guess it sweetens the situation, though I
> > really would like to know how to store in a binary format.
> 
> Here's one possibility, using the Storable class:
> 
> import Word
> import IOExts
> import Foreign.Ptr
> import Foreign.Storable
> import Foreign.Marshal.Alloc
> 
> toOctets :: Storable a => a -> [Word8]
> toOctets x = unsafePerformIO $ do
>     ptr <- malloc :: Storable a => IO (Ptr a)
>     poke ptr value
>     let bptr = castPtr ptr :: Ptr Word8
>     bytes <- mapM (peekElemOff bptr) [0 .. sizeOf x - 1] :: IO [Word8]
>     free ptr
>     return bytes
> 
> However, this will use the internal format, which will vary between
> architectures, so a file written on one architecture may not work on a
> different architecture.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe