Monday 30 May 2016

Haskell: Working with binary files

Haskell provides number of functions to work with binary files. These are similar to the functions defined to work with text files. Following table summarizes the functions used to work with binary files.

Function
Signature
Description
withBinaryFile
withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withOpenFile function takes a file, OPMode and function (Handle -> IO r)  as an argument and produce some result IO r. The handle will be closed on exit from withOpenFile. It is same as withFile.
openBinaryFile
openBinaryFile :: FilePath -> IOMode -> IO Handle
It is just like openFile, but opens file in binary mode.
hSetBinaryMode
hSetBinaryMode :: Handle -> Bool -> IO ()
Set the mode to given handle. If you pass Boolean argument as True, it sets binary mode, else text mode.
hPutBuf
hPutBuf :: Handle -> GHC.Ptr.Ptr a -> Int -> IO ()
hPutBuf hdl buf count writes count 8-bit bytes from the buffer buf to the handle hdl.
hGetBuf
hGetBuf :: Handle -> GHC.Ptr.Ptr a -> Int -> IO Int
hGetBuf hdl buf count reads data from the handle hdl into the buffer buf until either EOF is reached or count 8-bit bytes have been read. It returns the number of bytes actually read.
hGetBufSome
hGetBufSome :: Handle -> GHC.Ptr.Ptr a -> Int -> IO Int
hGetBufSome hdl buf count reads data from the handle hdl into the buffer buf. If there is any data available to read, then hGetBufSome returns it immediately; it only blocks if there is no data to be read.
hGetBufNonBlocking
hGetBufNonBlocking :: Handle -> GHC.Ptr.Ptr a -> Int -> IO Int
hGetBufNonBlocking hdl buf count reads data from the handle hdl into the buffer buf until either EOF is reached, or count 8-bit bytes have been read, or there is no more data available to read immediately.
hPutBufNonBlocking
hPutBufNonBlocking :: Handle -> GHC.Ptr.Ptr a -> Int -> IO Int
hPutBufNonBlocking hdl buf count writes data from the buffer to handle.

Following program reads a binary file and display the contents to terminal.


FileUtil.hs
import System.IO

getFileContents fileHandle = 
    do
     isEofFile <- hIsEOF fileHandle

     if isEofFile
        then return ()
        else
            do
                 info <- hGetLine fileHandle
                 putStrLn info
                 getFileContents fileHandle

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

Note
a.   hGetBuf blocks execution until either the specified number of bytes have been read or EOF is reached.
b.   hGetBufSome blocks execution until it can either read at least some bytes, or until EOF has been reached. It doesn't read more than the given number of bytes.
c.    hGetBufNonBlocking doesn't block execution. It tries to read the given number of bytes if any are available, but may return fewer.




Previous                                                 Next                                                 Home

No comments:

Post a Comment