Saturday 28 May 2016

Haskell: hFileSize : Get the size of file


System.IO module provides function hFileSize, which takes a file handle and return the size of that file in 8-bit bytes.

FileUtil.hs
import System.IO

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

        handle <- openFile fileName ReadMode
        size <- hFileSize handle
        putStrLn $ "Size of the file is " ++ (show size) ++ " bytes"


$ 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
Size of the file is 55 bytes
$
$ ./FileUtil
Enter file name (Including full path) to read
/Users/harikrishna_gurram/study1/Haskell/Haskell_excercises.docx
Size of the file is 144318 bytes

Previous                                                 Next                                                 Home

No comments:

Post a Comment