Reading in a binary file in haskell
Asked Answered
K

2

6

How could I write a function with a definition something like...

readBinaryFile :: Filename -> IO Data.ByteString

I've got the functional parts of Haskell down, but the type system and monads still make my head hurt. Can someone write and explain how that function works to me?

Katsuyama answered 9/8, 2010 at 6:6 Comment(0)
P
9
import Data.ByteString.Lazy
readFile fp

easy as pie man. Knock off the lazy if you don't want the string to be lazy.

import Data.ByteString.Lazy as BS
import Data.Word
import Data.Bits

fileToWordList :: String -> IO [Word8]
fileToWordList fp = do
    contents <- BS.readFile fp
    return $ unpack contents
Proclivity answered 9/8, 2010 at 6:17 Comment(0)
D
3

readBinaryFile :: Filename -> IO Data.ByteString

This is simply the Data.ByteString.readFile function, which you should never have to write, since it is in the bytestring package.

Dillard answered 22/4, 2011 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.