[Haskell-beginners] hGetContents exception

Daniel Fischer daniel.is.fischer at googlemail.com
Wed Sep 14 15:15:11 CEST 2011

On Wednesday 14 September 2011, 14:50:12, Luca Ciciriello wrote:
> Hi All.
> I'm using the function hGetContents in order to read some text file. If
> one or more of these text file have a wrong UTF encoding, I get the
> error:
> hGetContents: invalid argument (Illegal byte sequence)
> My workaround is to open the wrong encoded file in emacs and create a
> copy of this file (cut and paste in a new buffer). After this operation
> the new file has a correct UTF encoding and hGetContents doesn't
> complain any more.

Wouldn't using iconv be more convenient?

> Is there a better way to read (without complaining) such wrong file
> without an external action (emacs)?

If you know the encoding of the file, you can

hSetEncoding handle encoding

after you opened the file (if it's one of the known encodings).

More information about the Beginners mailing list