|
|
|
|
Synopsis |
|
|
|
Documentation |
|
|
Haskell defines operations to read and write characters from and 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 file system objects. A handle has at least the following properties:
- whether it manages input or output or both;
- whether it is open, closed or semi-closed;
- whether the object is seekable;
- whether buffering is disabled, or enabled on a line or block basis;
- a buffer (whose length may be zero).
Most handles will also have a current I/O position indicating where the next
input or output operation will occur. 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. Handles are in the Show and Eq classes. The string
produced by showing a handle is system dependent; it should include
enough information to identify the handle for debugging. A handle is
equal according to == only to itself; no attempt
is made to compare the internal state of different handles for equality.
GHC note: a Handle will be automatically closed when the garbage
collector detects that it has become unreferenced by the program.
However, relying on this behaviour is not generally recommended:
the garbage collector is unpredictable. If possible, use explicit
an explicit hClose to close Handles when they are no longer
required. GHC does not currently attempt to free up file
descriptors when they have run out, it is your responsibility to
ensure that this doesn't happen.
| Instances | |
|
|
|
Instances | |
|
|
|
Constructors | ReadMode | | WriteMode | | AppendMode | | ReadWriteMode | |
| Instances | |
|
|
|
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, or flushed,
from the internal buffer according to the buffer mode:
- line-buffering: the entire output buffer is flushed
whenever a newline is output, the buffer overflows,
a System.IO.hFlush is issued, or the handle is closed.
- block-buffering: the entire buffer is written out whenever it
overflows, a System.IO.hFlush is issued, or the handle is closed.
- no-buffering: output is written immediately, and never stored
in the buffer.
An implementation is free to flush the buffer more frequently,
but not less frequently, than specified above.
The output buffer is emptied as soon as it has been written out.
Similarly, input occurs according to the buffer mode for the handle:
- line-buffering: when the buffer for the handle is not empty,
the next item is obtained from the buffer; otherwise, when the
buffer is empty, characters up to and including the next newline
character are read into the buffer. No characters are available
until the newline character is available or the buffer is full.
- block-buffering: when the buffer for the handle becomes empty,
the next block of data is read into the buffer.
- no-buffering: the next input item is read and returned.
The System.IO.hLookAhead operation implies that even a no-buffered
handle may require a one-character buffer.
The default buffering mode when a handle is opened is
implementation-dependent and may depend on the file system object
which is attached to that handle.
For most implementations, physical files will normally be block-buffered
and terminals will normally be line-buffered.
| Constructors | NoBuffering | buffering is disabled if possible.
| LineBuffering | line-buffering should be enabled if possible.
| BlockBuffering (Maybe Int) | block-buffering should be enabled if possible.
The size of the buffer is n items if the argument
is Just n and is otherwise implementation-dependent.
|
| Instances | |
|
|
|
A mode that determines the effect of hSeek hdl mode i, as follows:
| Constructors | AbsoluteSeek | the position of hdl is set to i.
| RelativeSeek | the position of hdl is set to offset i
from the current position.
| SeekFromEnd | the position of hdl is set to offset i
from the end of the file.
|
| Instances | |
|
|
|
A handle managing input from the Haskell program's standard input channel.
|
|
|
A handle managing output to the Haskell program's standard output channel.
|
|
|
A handle managing output to the Haskell program's standard error channel.
|
|
|
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.
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 position is 0).
The initial buffer mode is implementation-dependent.
This operation may fail with:
- isAlreadyInUseError if the file is already open and cannot be reopened;
- isDoesNotExistError if the file does not exist; or
- isPermissionError if the user does not have permission to open the file.
Note: if you will be working with files containing binary data, you'll want to
be using openBinaryFile.
|
|
|
Computation hClose hdl makes handle hdl closed. Before the
computation finishes, if hdl is writable its buffer is flushed as
for hFlush.
Performing hClose on a handle that has already been closed has no effect;
doing so is not an error. All other operations on a closed handle will fail.
If hClose fails for any reason, any further operations (apart from
hClose) on the handle will still fail as if hdl had been successfully
closed.
|
|
|
For a handle hdl which attached to a physical file,
hFileSize hdl returns the size of that file in 8-bit bytes.
|
|
|
For a readable handle hdl, 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.
NOTE: hIsEOF may block, because it has to attempt to read from
the stream to determine whether there is any more data to be read.
|
|
|
The computation isEOF is identical to hIsEOF,
except that it works only on stdin.
|
|
|
Computation hSetBuffering hdl mode sets the mode of buffering for
handle hdl on subsequent reads and writes.
If the buffer mode is changed from BlockBuffering or
LineBuffering to NoBuffering, then
- if hdl is writable, the buffer is flushed as for hFlush;
- if hdl is not writable, the contents of the buffer is discarded.
This operation may fail with:
- isPermissionError if the handle has already been used for reading
or writing and the implementation does not allow the buffering mode
to be changed.
|
|
|
Computation hGetBuffering hdl returns the current buffering mode
for hdl.
|
|
|
The action hFlush hdl causes any items buffered for output
in handle hdl to be sent immediately to the operating system.
This operation may fail with:
- isFullError if the device is full;
- isPermissionError if a system resource limit would be exceeded.
It is unspecified whether the characters in the buffer are discarded
or retained under these circumstances.
|
|
|
Computation hGetPosn hdl returns the current I/O position of
hdl as a value of the abstract type HandlePosn.
|
|
|
If a call to hGetPosn hdl returns a position p,
then computation hSetPosn p sets the position of hdl
to the position it held at the time of the call to hGetPosn.
This operation may fail with:
- isPermissionError if a system resource limit would be exceeded.
|
|
|
Computation hSeek hdl mode i sets the position of handle
hdl depending on mode.
The offset i is given in terms of 8-bit bytes.
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 (for instance, 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.
This operation may fail with:
- isPermissionError if a system resource limit would be exceeded.
|
|
|
Computation hWaitForInput hdl t
waits until input is available on handle hdl.
It returns True as soon as input is available on hdl,
or False if no input is available within t milliseconds.
If t is less than zero, then hWaitForInput waits indefinitely.
This operation may fail with:
NOTE for GHC users: unless you use the -threaded flag,
hWaitForInput t where t >= 0 will block all other Haskell
threads for the duration of the call. It behaves like a
safe foreign call in this respect.
|
|
|
Computation hReady hdl indicates whether at least one item is
available for input from handle hdl.
This operation may fail with:
- System.IO.Error.isEOFError if the end of file has been reached.
|
|
|
Computation hGetChar hdl reads a character from the file or
channel managed by hdl, blocking until a character is available.
This operation may fail with:
|
|
|
Computation hGetLine hdl reads a line from the file or
channel managed by hdl.
This operation may fail with:
- isEOFError if the end of file is encountered when reading
the first character of the line.
If hGetLine encounters end-of-file at any other point while reading
in a line, it is treated as a line terminator and the (partial)
line is returned.
|
|
|
Computation hLookAhead returns the next character from the handle
without removing it from the input buffer, blocking until a character
is available.
This operation may fail with:
- isEOFError if the end of file has been reached.
|
|
|
Computation hGetContents hdl returns the list of characters
corresponding to the unread portion of the channel or file managed
by hdl, which is put 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
list returned by hGetContents hdl.
Any operation that fails because a handle is closed,
also fails if a handle is semi-closed. The only exception is hClose.
A semi-closed handle becomes closed:
- if hClose is applied to it;
- if an I/O error occurs when reading an item from the handle;
- or once the entire contents of the handle has been read.
Once a semi-closed handle becomes closed, the contents of the
associated list becomes fixed. The contents of this final list is
only partially specified: it will contain at least all the items of
the stream that were evaluated prior to the handle becoming closed.
Any I/O errors encountered while a handle is semi-closed are simply
discarded.
This operation may fail with:
|
|
|
Computation hPutChar hdl ch writes the character ch to the
file or channel managed by hdl. Characters may be buffered if
buffering is enabled for hdl.
This operation may fail with:
|
|
|
Computation hPutStr hdl s writes the string
s to the file or channel managed by hdl.
This operation may fail with:
|
|
|
The same as hPutStr, but adds a newline character.
|
|
|
Computation hPrint hdl t writes the string representation of t
given by the shows function to the file or channel managed by hdl
and appends a newline.
This operation may fail with:
- System.IO.Error.isFullError if the device is full; or
- System.IO.Error.isPermissionError if another system resource limit would be exceeded.
|
|
|
|
|
|
|
|
|
|
|
|
|
An error indicating that an IO operation failed because
one of its arguments already exists.
|
|
|
An error indicating that an IO operation failed because
one of its arguments does not exist.
|
|
|
An error indicating that an IO operation failed because
one of its arguments is a single-use resource, which is already
being used (for example, opening the same file twice for writing
might give this error).
|
|
|
An error indicating that an IO operation failed because
the device is full.
|
|
|
An error indicating that an IO operation failed because
the end of file has been reached.
|
|
|
An error indicating that an IO operation failed because
the operation was not possible.
Any computation which returns an IO result may fail with
isIllegalOperation. In some cases, an implementation will not be
able to distinguish between the possible error causes. In this case
it should fail with isIllegalOperation.
|
|
|
An error indicating that an IO operation failed because
the user does not have sufficient operating system privilege
to perform that operation.
|
|
|
A programmer-defined error value constructed using userError.
|
|
|
|
|
|
|
|
|
The construct try comp exposes IO errors which occur within a
computation, and which are not fully handled.
Non-I/O exceptions are not caught by this variant; to catch all
exceptions, use Control.Exception.try from Control.Exception.
|
|
|
The bracket function captures a common allocate, compute, deallocate
idiom in which the deallocation step must occur even in the case of an
error during computation. This is similar to try-catch-finally in Java.
This version handles only IO errors, as defined by Haskell 98.
The version of bracket in Control.Exception handles all exceptions,
and should be used instead.
|
|
|
A variant of bracket where the middle computation doesn't want x.
This version handles only IO errors, as defined by Haskell 98.
The version of bracket_ in Control.Exception handles all exceptions,
and should be used instead.
|
|
|
A value of type IO a is a computation which, when performed,
does some I/O before returning a value of type a.
There is really only one way to "perform" an I/O action: bind it to
Main.main in your program. When your program is run, the I/O will
be performed. It isn't possible to perform I/O from an arbitrary
function, unless that function is itself in the IO monad and called
at some point, directly or indirectly, from Main.main.
IO is a monad, so IO actions can be combined using either the do-notation
or the >> and >>= operations from the Monad class.
| Instances | |
|
|
|
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.
|
|
|
The Haskell 98 type for exceptions in the IO monad.
Any I/O operation may raise an IOError instead of returning a result.
For a more general type of exception, including also those that arise
in pure code, see Control.Exception.Exception.
In Haskell 98, this is an opaque type.
|
|
|
Raise an IOError in the IO monad.
|
|
|
Construct an IOError value with a string describing the error.
The fail method of the IO instance of the Monad class raises a
userError, thus:
instance Monad IO where
...
fail s = ioError (userError s)
|
|
|
The catch function establishes a handler that receives any IOError
raised in the action protected by catch. An IOError is caught by
the most recent handler established by catch. These handlers are
not selective: all IOErrors are caught. Exception propagation
must be explicitly provided in a handler by re-raising any unwanted
exceptions. For example, in
f = catch g (\e -> if IO.isEOFError e then return [] else ioError e)
the function f returns [] when an end-of-file exception
(cf. isEOFError) occurs in g; otherwise, the
exception is propagated to the next outer handler.
When an exception propagates outside the main program, the Haskell
system prints the associated IOError value and exits the program.
Non-I/O exceptions are not caught by this variant; to catch all
exceptions, use Control.Exception.catch from Control.Exception.
|
|
|
The interact function takes a function of type String->String
as its argument. The entire input from the standard input device is
passed to this function as its argument, and the resulting string is
output on the standard output device.
|
|
|
Write a character to the standard output device
(same as hPutChar stdout).
|
|
|
Write a string to the standard output device
(same as hPutStr stdout).
|
|
|
The same as putStr, but adds a newline character.
|
|
|
The print function outputs a value of any printable type to the
standard output device.
Printable types are those that are instances of class Show; print
converts values to strings for output using the show operation and
adds a newline.
For example, a program to print the first 20 integers and their
powers of 2 could be written as:
main = print ([(n, 2^n) | n <- [0..19]])
|
|
|
Read a character from the standard input device
(same as hGetChar stdin).
|
|
|
Read a line from the standard input device
(same as hGetLine stdin).
|
|
|
The getContents operation returns all user input as a single string,
which is read lazily as it is needed
(same as hGetContents stdin).
|
|
|
The readFile function reads a file and
returns the contents of the file as a string.
The file is read lazily, on demand, as with getContents.
|
|
|
The computation writeFile file str function writes the string str,
to the file file.
|
|
|
The computation appendFile file str function appends the string str,
to the file file.
Note that writeFile and appendFile write a literal string
to a file. To write a value of any printable type, as with print,
use the show function to convert the value to a string first.
main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])
|
|
|
The readIO function is similar to read except that it signals
parse failure to the IO monad instead of terminating the program.
|
|
|
The readLn function combines getLine and readIO.
|
|
Produced by Haddock version 2.6.1 |