Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cat emulator in Haskell

Tags:

haskell

So I'm attempting to make a program that reads from a handle and writes to stdOut, like so:

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'm fairly new to Hs and it seems like this should be far more simple than I'm making it for myself. Any suggestions to helping me move further?

the usage would be ./cat "foo.txt" and would print the text in the foo.txt file to stdOut.

like image 788
Sam P Avatar asked Jan 17 '26 09:01

Sam P


1 Answers

There is the below function which does what you want.

readFile :: FilePath -> IO String

use this with putStr to print the IO String

like image 84
Ankur Avatar answered Jan 20 '26 01:01

Ankur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!