[Haskell-cafe] binary file handling (was: no subject)

Paul R. Potts paul at thepottshouse.org
Thu May 24 12:14:08 EDT 2007


Hello all,

Ketil Malde wrote:

> This is what I find in System.IO (ghci> :b System.IO):
>
>   openBinaryFile :: FilePath -> IOMode -> IO Handle
>   openBinaryTempFile :: FilePath -> String -> IO (FilePath, Handle)
>   hSetBinaryMode :: Handle -> Bool -> IO ()
>
> so you have the option of either using openBinaryFile or openFile and
> using hSetBinaryMode to true. I guess - I've never had to use them.
>
> I can't find a readBinaryFile either, but writing one might be a good
> excercise?
>
> Makes me wonder whether one should have binary be the default?  I'm a
> stranger in Windows-land, but are there cases where you want reading of
> a file to be terminated on ^Z?  Seems pretty awful to me.

I ran into this same problem when trying to write a binary file filter; see:

http://praisecurseandrecurse.blogspot.com/2007/04/haskell-for-short-attention-span-simple.html

Back then I got some help on #haskell to figure out how to write
ReadBinaryFile. I got that working, and wrote

"This works fine, although I would humbly suggest that GHC's standard
library should provide easy access to binary files; while the solution is
pretty trivial, it is a wart."

I would not go so far as to say that binary should be the default mode for
opening and reading a file, but I would say that the two should be made
equally simple and clear options in the standard library.

Paul R. Potts

-- 
Paul R. Potts - paul at thepottshouse.org - http://thepottshouse.org




More information about the Haskell-Cafe mailing list