Reading a file encoded with "US-ASCII" in Haskell: hGetContents: invalid argument (invalid byte sequence)

I use Haskell to program the parser, but this error is a wall that I cannot get through. Here is my code:

main = do
  arguments    <- getArgs
  let fileName = head arguments
  fileContents <- readFile fileName
  converter    <- open "UTF-8" Nothing
  let titleLength           = length fileName
      titleWithoutExtension = take (titleLength - 4) fileName
      allNonEmptyLines      = unlines $ tail $ filter (/= "") $ lines fileContents

When I try to read a file encoded with "US-ASCII", I get a known hGetContents error: invalid argument (invalid byte sequence). I tried changing "UTF-8" in my code to "US-ASCII", but the error persists. Is there any way to read these files or any problems with file encoding?

+4
source share
1 answer

hSetEncoding , :

import System.Environment
import System.IO

main = do
  (path : _) <- getArgs
  h <- openFile path ReadMode
  hSetEncoding h latin1
  contents <- hGetContents h
  -- no need to close h
  putStrLn $ show $ length contents

, ASCII, UTF8, 1 - , .

+5

Source: https://habr.com/ru/post/1614796/


All Articles