Haskell readFile illegal Char
I need to read a File containing chars like 'ç' ou 'á'... The Problem is that when I try to readFile a txt file, GHC returns: illegal byte sequence. Is there a way around this?
main = do putStr "Insert file path\n开发者_开发技巧"
a <- getLine
x <- readFile a
print x
Main> main
Main> Insert file path
Main> /Users/$HOME/Desktop/File.txt
Main> (illegal byte sequence)
Thanks
Data.ByteString.readFile
reads the file as a raw stream of bytes, whereas System.IO.readFile
uses localeEncoding
to decode characters and will throw exceptions if the contents of the file cannot be decoded with your current locale.
If you want to continue to use String
instead of ByteString
+decode, and you know the encoding of the file, you can specify it with
do handle <- openFile a ReadMode
hSetEncoding handle latin1 -- or whatever applies
x <- hGetContents handle
精彩评论