Haskell cat emulator

So, I'm trying to make a program that reads from a descriptor and writes to stdOut, for example:

import IO
import System

cat w   =   do  
            fromHandle  <- getAndOpenFile w ReadMode
            contents    <- hGetContents fromHandle
            putStr contents
            putStr "Done."

getAndOpenFile  :: String -> IOMode -> IO Handle

getAndOpenFile name mode =
      do
      catch (openFile name mode)
        (\_ -> do   
                putStrLn ("Cannot open "++ name ++"\n")
                return())

I am new to Hs and it seems like it should be a lot easier than I do it for myself. Any suggestions that will help me move on?

use will be. / cat "foo.txt" and will display the text in the foo.txt file in stdOut.

+3
source share
1 answer

There is a function below that does what you want.

readFile :: FilePath -> IO String

use this with putStr to print an input / output string

+4
source

All Articles