Sunday 29 May 2016

Haskell: hIsWritable: Check whether file is writable (or) not

hIsWritable takes a file handle and return IO True, if the handle is writable, else IO False.

Prelude System.IO> :t hIsWritable
hIsWritable :: Handle -> IO Bool


FileUtil.hs
import System.IO

main = do
        putStrLn "Enter file name (Including full path) to read"
        fileName <- getLine
        fileHandle <- openFile fileName ReadMode

        isReadable <- hIsReadable fileHandle
        isWritable <- hIsWritable fileHandle

        putStrLn $ "Is file handle readable " ++ show isReadable
        putStrLn $ "Is file handle writable " ++ show isWritable

$ runghc FileUtil.hs
Enter file name (Including full path) to read
today.txt
Is file handle readable True
Is file handle writable False



Previous                                                 Next                                                 Home

No comments:

Post a Comment