Sunday, 29 May 2016

Haskell: hIsClosed: Check whether file handle is closed or not

hIsClosed function takes a file handle as argument and return IO True, if the file handle is closed, else IO False.

Prelude System.IO> :t hIsClosed
hIsClosed :: 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

        isHandleOpen <- hIsClosed fileHandle
        putStrLn $ "Is handle closed : " ++ show isHandleOpen

        hClose fileHandle
        isHandleOpen <- hIsClosed fileHandle
        putStrLn $ "Is handle closed : " ++ show isHandleOpen

$ ghc FileUtil.hs
[1 of 1] Compiling Main             ( FileUtil.hs, FileUtil.o )
Linking FileUtil ...
$ 
localhost:programs harikrishna_gurram$ ./FileUtil
Enter file name (Including full path) to read
today.txt
Is handle closed : False
Is handle closed : True



Previous                                                 Next                                                 Home

No comments:

Post a Comment