[Haskell-cafe] hClose: invalid argument (Invalid or incomplete multibyte or wide character)

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Wed Oct 6 03:42:52 EDT 2010


Hi,

Daniel Fischer wrote:
> On Tuesday 05 October 2010 23:34:56, Johannes Waldmann wrote:
> > main =  writeFile "check.out" "ü"
> >
> > that's u-umlaut, and the source file is utf-8-encoded
> > and ghc-6.12.3 compiles it without problems but when running, I get
> >
> > hClose: invalid argument (Invalid or incomplete multibyte or wide
> > character)

In order to make the behaviour independent of the locale (which is
desirable for programs storing state in text files), you can use
functions like writeFileUTF8 and readFileUTF8 here:

    import System.IO

    writeFileUTF8 file text = withFile file WriteMode $ \handle -> do
        hSetEncoding handle utf8
        hPutStr handle text

    readFileUTF8 file = do
        handle <- openFile file ReadMode
        hSetEncoding handle utf8
        hGetContents handle

    main = do
        let s = "äöü"
        writeFileUTF8 "test.out" s
        s' <- readFileUTF8 "test.out"
        putStrLn $ unwords [s, "==", s']

Of course using System.IO.UTF8 from utf8-string would also work.

HTH,

Bertram


More information about the Haskell-Cafe mailing list