Reading/Writing Binary Data in Haskell

Glynn Clements glynn.clements@virgin.net
Thu, 10 Jul 2003 03:56:55 +0100


Wolfgang Jeltsch wrote:

> > Both GHC and Hugs provide openFileEx, which allows files to be read in
> > binary mode (without EOL/EOF translations).
> 
> So we have portable binary I/O, don't we?
> 
> By the way, does one still read characters rather than bytes even if using 
> openFileEx?

openFileEx returns a Handle just like openFile, and functions such as
hGetChar return a Char regardless of where the Handle came from. OTOH,
those "Char"s are still just octets stored in an oversized container.

> Is it guaranteed that one character corresponds to one byte when 
> using openFileEx?

The report says almost nothing about the semantics of I/O. AFAICT, an
implementation could write the data in EBCDIC without violating the
letter of the report.

OTOH, existing implementations (at least GHC and Hugs) currently read
and write "8-bit binary", i.e. characters 0-255 get read and written
"as-is" and anything else breaks, and changing that would probably
break a fair amount of existing code.

-- 
Glynn Clements <glynn.clements@virgin.net>