开发者

cat emulator in 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.


There is the below function which does what you want.

readFile :: FilePath -> IO String

use this with putStr to print the IO String

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜