[Haskell-cafe] iteratee enumHandle
hellish at comcast.net
Thu Jul 9 22:49:14 EDT 2009
What's happening is reading [Char] uses the Storable instance for Char
which is 32-bit. Thus, you get gibberish. The below does what you want,
by reading Word8s and converting them.
main :: IO ()
main = do
h <- openFile "mamma23" ReadWriteMode
hPutStr h "ciao"
hSeek h AbsoluteSeek 0
l <- enumHandle h readString >>= run
print $ assert (l == "ciao") ()
-- This is declared on its own so I can give a type signature without making
-- any of the above lines unmanageably long.
readString :: IterateeG  Word8 IO String
readString = joinI $ mapStream (chr . fromIntegral) stream2list
This only works for ASCII, of course. Someone should write some
enumerators for the other encodings.
More information about the Haskell-Cafe