Binary IO

John Meacham john at
Thu Apr 21 06:16:18 EDT 2005

On Thu, Apr 21, 2005 at 10:32:59AM +0100, Simon Marlow wrote:
> On 20 April 2005 22:35, Scott Turner wrote:
> > On 2005 April 20 Wednesday 15:56, Iavor Diatchki wrote:
> >> I've been playing around with decoding binary files, and I couldn't
> >> find a function in the libraries to read a byte from a file. Does
> >> anyone know if there is such a function?  ....... it seems
> >> that we should have:
> >> hGetByte :: Handle -> IO Word8
> > 
> > hGetByte could be implemented on top of hGetBuf and StorableArray,
> > but the overhead would be ugly.  Recently I needed binary input for
> > similar reasons, and would have used hGetByte had it been available. 
> > I ended up performing input using a reasonable-sized buffer.
> In GHC, you can assume that 
>   hGetByte h = do c <- hGetChar h; return (fromIntegral (ord c))
> you must open the file in binary mode (openBinaryFile).  I don't think
> this works with Hugs, though.
> Personally, I think this ought to be part of the redesign of the I/O
> library.  hGetByte and hPutByte would be a stopgap.  But, not much
> progress is being made on an I/O redesign right now.

but a useful stopgap, because their use would guarentee your code
wouldn't suddenly stop working when ghc changes its IO system.

I'd like to see all of:
readBinaryFile :: String -> IO [Word8]
writeBinaryFile :: String -> [Word8] -> IO ()
hGetByte :: Handle -> IO Word8
hPutByte :: Handle -> Word8 -> IO ()

added to all the compilers. simply so there is a straightforward (if
not ideal) portable way to do binary IO. We can worry about the ideal pecfect low
level interface later.


John Meacham - ⑆⑆john⑈ 

More information about the Libraries mailing list