Sunday 29 May 2016

Haskell: hIsOpen: Check whether file handle is open (or) not

hIsOpen function takes a file handle and return IO True, if the handle is still in open state, else IO False.

Prelude System.IO> :t hIsOpen
hIsOpen :: 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 <- hIsOpen fileHandle
        putStrLn $ "Is handle open : " ++ show isHandleOpen

        hClose fileHandle
        isHandleOpen <- hIsOpen fileHandle
        putStrLn $ "Is handle open : " ++ show isHandleOpen

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



Previous                                                 Next                                                 Home

No comments:

Post a Comment