Haskell interfaces to the external world through an abstract file
system. This file system is a collection of named file
system objects, which may be organised
in directories (see LibDirectory
).
In some implementations, directories may themselves be file system
objects and could be entries in other directories. For simplicity,
any non-directory file system object is termed a file,
although it could in fact be a communication channel, or any other
object recognised by the operating system. Physical files are
persistent, ordered files, and normally reside on disk.
File and directory names are values of type String
, whose
precise meaning is operating system dependent. Files can be opened,
yielding a handle which can then be used to operate on the contents
of that file.
> interface PreludeStdIO where
> import PreludeMonadicIO > import PreludeIOError
> type Handle > type FilePath = String
Handles
The standard defines operations to read/write finite sequences of
items from/to files, represented by values of type
Handle
. Each value of this type is a handle: a
record used by the Haskell run-time system to manage I/O with
operating system objects.
A handle has at least the following properties:
A handle is readable if it manages only input or both input
and output; likewise, it is writable if it manages only
output or both input and output. A handle is open when first
allocated. Once it is closed it can no longer be used for either
input or output, though an implementation cannot re-use its storage
while references remain to it
.
Semi-Closed Handles
The operation hGetContents
puts a handle hdl into
an intermediate state, semi-closed. In this state, hdl is
effectively closed, but items are read from hdl on demand
and accumulated in a special stream returned
by hGetContents
hdl.
Any operation except for hClose
that fails because a
handle is closed, also fails if a handle is semi-closed. A
semi-closed handle becomes closed:
hClose
is applied to
it
Once a semi-closed handle becomes closed, the contents of the
associated stream becomes fixed, and is the list of those items which
were successfully read from that handle. Any I/O errors encountered when
a handle is semi-closed are simply discarded.
> stdin, stdout, stderr :: Handle
Three handles are allocated during program initialisation .
The first two manage input or output from the Haskell program's
standard input or output channel respectively
. The third manages
output to the standard error channel
.
These handles are
initially open.
Opening and Closing Files
Opening Files
> data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode > openFile :: FilePath -> IOMode -> IO Handle
Computation openFile
file mode allocates
and returns a new, open handle to manage the file
file.
It manages input if mode is
ReadMode
, output if mode is WriteMode
or AppendMode
, and both input and output if mode is
ReadWriteMode
.
If the file does not exist and it is opened for output, it should be
created as a new file. If mode is WriteMode
and the file already exists, then it should be truncated to zero
length (note: some operating systems delete empty files, so there is
no guarantee that the file will exist following an
openFile
with mode WriteMode
unless it is subsequently written to successfully). The handle is
positioned at the end of the file if mode is
AppendMode
, and otherwise at the beginning (in which case
its internal I/O position is 0).
The initial buffer mode is implementation-dependent.
The computation may fail with:
HardwareFault
EIO
]
PermissionDenied
EACCES
]
ResourceExhausted
EMFILE
, ENFILE
, EAGAIN
]
InvalidArgument
ENAMETOOLONG
, ELOOP
]
NoSuchThing
ENOENT
, ENOTDIR
, ENXIO
]
InappropriateType
EISDIR
]
If openFile
fails on a file opened for output, the file may
still have been created if it did not already exist.
Implementations should enforce, locally to the Haskell process, multiple-reader single-writer locking on files, which is to say that there may either be many handles on the same file which manage input, or just one handle on the file which manages output. If any open or semi-closed handle is managing a file for output, no new handle can be allocated for that file. If any open or semi-closed handle is managing a file for input, new handles can only be allocated if they do not manage output.
Two physical files are the same if they have the same absolute name.
An implementation is free to impose stricter conditions.
Closing Files
> hClose :: Handle -> IO ()
Computation hClose
hdl makes handle
hdl closed. Before the computation finishes, if hdl is
writable its buffer is flushed as for
hFlush
.
The computation may fail with:
HardwareFault
EIO
]
ResourceExhausted
ENOSPC
]
PermissionDenied
EFBIG
]
ResourceVanished
EPIPE
]
IllegalOperation
If the operation fails for any reason, any further operations on the handle will still fail as for a closed handle.
Determining the Size of a File
> hFileSize :: Handle -> IO Integer
For a handle hdl which attached to a physical file,
hFileSize
hdl returns the size of
that file in 8-bit bytes (>= 0).
The computation may fail with:
HardwareFault
EIO
]
InappropriateType
IllegalOperation
> hIsEOF :: Handle -> IO Bool > isEOF :: IO Bool > isEOF = hIsEOF stdin
For a readable handle hdl, computation hIsEOF
hdl returns True
if no further input can be
taken from hdl or for a physical file, if the current I/O
position is equal to the length of the file. Otherwise, it returns
False
.
The computation may fail with:
HardwareFault
EIO
]
ResourceExhausted
ENOMEM
]
IllegalOperation
Buffering Operations
Three kinds of buffering are supported: line-buffering,
block-buffering or no-buffering. These modes have the following effects.
For output, items are written out from the internal buffer
according to the buffer mode:
Similarly, input occurs according to the buffer mode for handle hdl.
For most implementations, physical files will normally be block-buffered and terminals will normally be line-buffered.
> data BufferMode = NoBuffering | LineBuffering | BlockBuffering (Maybe Int) > hSetBuffering :: Handle -> BufferMode -> IO ()
Computation hSetBuffering
hdl mode sets the
mode of buffering for handle hdl on subsequent reads and writes.
LineBuffering
, line-buffering is
enabled if possible.
BlockBuffering
size, then block-buffering
is enabled if possible. The size of the buffer is n items
if size is Just
n and is otherwise implementation-dependent.
NoBuffering
, then buffering is disabled if possible.
If the buffer mode is changed from BlockBuffering
or
LineBuffering
to NoBuffering
, then
hFlush
;
The default buffering mode when a handle is opened is implementation-dependent and may depend on the object which is attached to that handle.
The computation may fail with:
ResourceExhausted
ENOMEM
]
IllegalOperation
UnsupportedOperation
Flushing Buffers
> hFlush :: Handle -> IO ()
Computation hFlush
hdl causes any items
buffered for output in handle hdl to be sent immediately to
the operating system.
The computation may fail with:
HardwareFault
EIO
]
ResourceExhausted
ENOSPC
]
PermissionDenied
EFBIG
]
ResourceVanished
EPIPE
]
IllegalOperation
> data HandlePosn
> hGetPosn :: Handle -> IO HandlePosn
> hSetPosn :: HandlePosn -> IO ()
Computation hGetPosn
hdl returns the current I/O
position of hdl as an abstract value. Computation
hSetPosn
p sets the position of hdl
to a previously obtained position p.
The computation hSetPosn
may fail with:
HardwareFault
EIO
]
ResourceExhausted
ENOSPC
]
PermissionDenied
EFBIG
]
ResourceVanished
EPIPE
]
IllegalOperation
AppendMode
or the handle is closed.
UnsupportedOperation
> data SeekMode = AbsoluteSeek | RelativeSeek | SeekFromEnd
> hSeek :: Handle -> SeekMode -> Integer -> IO ()
Computation hSeek
hdl mode i sets the position of handle
hdl depending on mode
. If mode is
If hdl is block- or line-buffered, then seeking to a position which is not in the current buffer will first cause any items in the output buffer to be written to the device, and then cause the input buffer to be discarded.
Some handles may not be seekable (see hIsSeekable
),
or only support a subset of the possible positioning operations
(e.g. it may only be possible to seek to the end of a tape, or to a
positive offset from the beginning or current position).
It is not possible to set a negative I/O position, or for a physical file, an I/O position beyond the current end-of-file.
The computation may fail with:
HardwareFault
EIO
]
ResourceExhausted
ENOSPC
]
PermissionDenied
EFBIG
]
ResourceVanished
EPIPE
]
IllegalOperation
AppendMode
or the handle is closed.
UnsupportedOperation
> hIsOpen :: Handle -> IO Bool > hIsClosed :: Handle -> IO Bool > hIsReadable :: Handle -> IO Bool > hIsWritable :: Handle -> IO Bool > hIsBlockBuffered :: Handle -> IO (Bool,Maybe Int) > hIsLineBuffered :: Handle -> IO Bool > hIsNotBuffered :: Handle -> IO Bool > hIsSeekable :: Handle -> IO Bool
A number of operations return information about the properties of a
handle. Each of these operations
except hIsBlockBuffered
returns True
if the handle
has the specified property,
and False
otherwise.
Computation hIsBlockBuffered
hdl returns
( False, Nothing )
if hdl is not block-buffered.
Otherwise it returns ( True,
size )
,
where size is Nothing
for default buffering,
and ( Just
n )
for block-buffering
of n 8-bit bytes.
Any of the latter six computations may fail with
IllegalOperation