Skip to content

Instantly share code, notes, and snippets.

@fishyFrogFace
Created April 13, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fishyFrogFace/47bbbbeae66ddbdabf71cf7a78d6d566 to your computer and use it in GitHub Desktop.
Save fishyFrogFace/47bbbbeae66ddbdabf71cf7a78d6d566 to your computer and use it in GitHub Desktop.
Example of reading and writing to files with custom encoding
import System.IO
import Control.Applicative (liftA)
import Data.Foldable (for_)
import Control.DeepSeq (($!!))
writeFileWithEncoding :: FilePath -> String -> IO ()
writeFileWithEncoding fp content =
withFile fp WriteMode $ \h ->
hSetEncoding h utf8 >>
hPutStr h content
readFileWithEncoding :: FilePath -> IO String
readFileWithEncoding fp =
withFile fp ReadMode $ \h -> do
hSetEncoding h utf8
f <- hGetContents h
return $!! f
removeEnd :: String -> String
removeEnd (x:xs)
| x == '<' = []
| otherwise = x : removeEnd xs
removeStart :: String -> String
removeStart (x:xs)
| x == '>' = removeEnd xs
| otherwise = removeStart xs
addThings :: String -> String
addThings str = " (DEFAULT, " ++ str ++ ", 1)"
main :: IO ()
main = do
file <- liftA lines $ readFileWithEncoding "bygninger.txt"
let result = unlines $ map (addThings . removeStart) file
writeFileWithEncoding "res.txt" result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment