In this post, I am going to explain how
to read contents of a file. First we need to open a file, Haskell provides two
functions openFile, openBinaryFile to get a file handle. ‘openFile’ is used to
work with normal text files, where as openBinaryFile is used to work with
binary files.
Prelude> import System.IO Prelude System.IO> :t openFile openFile :: FilePath -> IOMode -> IO Handle Prelude System.IO> Prelude System.IO> :t openBinaryFile openBinaryFile :: FilePath -> IOMode -> IO Handle
Observe the type signature of openFile
and openBinaryFile functions. They are taking FilePath, IOMode as arguments.
FilePath is a synonym to String, it is defined like below.
type FilePath = String
openFile :: FilePath -> IOMode ->
IO Handle
openBinaryFile :: FilePath -> IOMode
-> IO Handle
IPMode specifies in which mode file should
be opened. Following table summarizes the modes.
IOMode
|
Is
Readable
|
Is
Writable
|
Description
|
ReadMode
|
Y
|
N
|
Open file in read mode. File must
exist already.
|
WriteMode
|
N
|
Y
|
Open file in write mode, override he
existing file contents. If file is not existed, create new one.
|
AppendMode
|
N
|
Y
|
Write data to the end of file. File is
created if it don’t exist.
|
ReadWriteMode
|
Y
|
Y
|
File can be readable and writable,
Create file, if it don’t exist.
|
openFile method gives a file handle, You
can use hGetLine method, which takes this file handle as argument and reads a
line from the file.
Prelude System.IO> :t hGetLine
hGetLine :: Handle -> IO String
Once you read the file completely you
need to close the file handle. ‘hClose’ function is used to close file handle.
Prelude System.IO> :t hClose
hClose :: Handle -> IO ()
Following is the complete working
application, that reads file contents.
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 <- openFile fileName ReadMode getFileContents fileHandle hClose fileHandle
$ runghc FileUtil.hs Enter file name (Including full path) to read /Users/harikrishna_gurram/quotes.txt "If you love a flower, don’t pick it up. Because if you pick it up it dies and it ceases to be what you love. So if you love a flower, let it be. Love is not about possession. Love is about appreciation." "Experience life in all possible ways -- good-bad, bitter-sweet, dark-light, summer-winter. Experience all the dualities. Don't be afraid of experience, because the more experience you have, the more mature you become."
When you are using openFile function,
you main get errors like isAlreadyInUseError, isDoesNotExistError,
isPermissionError. Following table summarizes the errors.
Error
|
Description
|
isAlreadyInUseError
|
If the file is already open and cannot
be reopened
|
isDoesNotExistError
|
If the file does not exist
|
isPermissionError
|
If the user does not have permission
to open the file.
|
No comments:
Post a Comment