[Haskell-beginners] Newbie unicode utf8 question
Manfred Lotz
manfred.lotz at arcor.de
Sun Oct 23 14:52:18 CEST 2011
Hi there,
I want to read a unicode file and print it to screen. My terminal was
unintentionally set to POSIX locale and that was the reason that it
didn't work properly.
When I added two hSetEncoding statements it worked fine, even under
POSIX.
Test file uc.in contains a simple line
Möbius Château
Here is the small test program I tried:
<-----------------------------snip---------------------------->
module Main where
import System.IO
import Text.Printf
main :: IO ()
main = do
h <- openFile "uc.in" ReadMode
hSetEncoding h utf8
hSetEncoding stdout utf8
s <- hGetContents h
print s
putStrLn $ "File contains line: " ++ s
printf "File contains line: %s\n" s
<-----------------------------snap---------------------------->
Both putStrLn, and printf work fine.
When setting a different locale, e.g. en_GB.iso88591 then both putStrLn
and printf work fine even if I haven't set encoding to utf in the
source.
Then I discovered System.IO.UTF8. However, when I use this as below
then the output is garbled.
<-----------------------------snip---------------------------->
module Main where
import System.IO
import System.IO.UTF8 as U
import Text.Printf
main :: IO ()
main = do
h <- openFile "uc.in" ReadMode
hSetEncoding stdout utf8
s <- U.hGetContents h
U.print s
U.putStrLn $ "File contains line: " ++ s
printf "File contains line: %s\n" s
<-----------------------------snap---------------------------->
Any idea why this won't work.
--
Manfred
More information about the Beginners
mailing list