[Haskell-cafe] Reading/writing packed bytes from file

Bulat Ziganshin bulat.ziganshin at gmail.com
Wed Jun 20 10:37:36 EDT 2007

Hello Jefferson,

Wednesday, June 20, 2007, 12:20:28 AM, you wrote:

> 4-byte int (count),
> (count) 2-byte unsigned shorts,
> (count) 4-byte floats

using my Streams package ( http://haskell.org/haskellwiki/Library/AltBinary ):

import Data.AltBinary
readall recordcount h = do
  replicateM recordcount $ do
    count <- getWord32le h
    keys   <- replicateM count (getWord16le h :: IO Int)
    values <- replicateM count (getFloat h)
    return (IntMap.fromList (zip keys values))

This isn't lazy and not tested

> all in little-endian order.  What I want to do is read each record
> (lazily), and unpack it into Data.IntMap.IntMap Float where the unsigned
> shorts become the keys and the 4-byte floats become the values.

> Then I want to do a lot of interesting processing which we'll skip here,
> and write back out packed data to a file in the format of

> 4-byte float,
> 4-byte float,
> 4-byte float

> for each record.

use either putFloat or define structure of 3 floats:

data F = F Float Float Float

and put entire F to the stream:

mapM_ put_ (IntMap.values your_map)

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list