Home > Mobile >  Taking a path from stdin and using it as argument to readFile
Taking a path from stdin and using it as argument to readFile

Time:02-19

As a learning strategy, I tried to modify Prelude interact function.

test.hs contents

interact' :: (String -> String) -> IO ()
interact' f = do
  path <- getContents
  s <- readFile path
  putStr (f s)

main :: IO ()
main = interact' id

test.txt contents

Hello World

Calling echo "test.txt" | runhaskell test.hs doesn't display Hello World. Am I overlooking something ?

CodePudding user response:

echo "test.txt" produces the string test.txt\n, and you don't have a file called test.txt\n, just one called test.txt. Either use getLine instead of getContents in Haskell to not get the newline, or printf instead of echo in the shell to not emit the newline in the first place.

  • Related