IO
- data Handle
- HandlePosn
- IOMode
- data BufferMode
- = NoBuffering
- | LineBuffering
- | BlockBuffering (Maybe Int)
- SeekMode
- stdin
- stdout
- stderr
- openFile
- hClose
- hFileSize
- hIsEOF
- isEOF
- hSetBuffering
- hGetBuffering
- hFlush
- hGetPosn
- hSetPosn
- hSeek
- hWaitForInput
- hReady :: Handle -> IO Bool
- hGetChar
- hGetLine
- hLookAhead
- hGetContents
- hPutChar
- hPutStr
- hPutStrLn
- hPrint :: Show a => Handle -> a -> IO ()
- hIsOpen
- hIsClosed
- hIsReadable
- hIsWritable
- hIsSeekable
- isAlreadyExistsError :: IOError -> Bool
- isDoesNotExistError :: IOError -> Bool
- isAlreadyInUseError :: IOError -> Bool
- isFullError :: IOError -> Bool
- isEOFError :: IOError -> Bool
- isIllegalOperation :: IOError -> Bool
- isPermissionError :: IOError -> Bool
- isUserError :: IOError -> Bool
- ioeGetErrorString :: IOError -> String
- ioeGetHandle :: IOError -> Maybe Handle
- ioeGetFileName :: IOError -> Maybe FilePath
- try :: IO a -> IO (Either IOError a)
- bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
- bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
- data IO a
- type FilePath = String
- type IOError = IOException
- ioError :: IOError -> IO a
- userError :: String -> IOError
- catch :: IO a -> (IOError -> IO a) -> IO a
- interact :: (String -> String) -> IO ()
- putChar :: Char -> IO ()
- putStr :: String -> IO ()
- putStrLn :: String -> IO ()
- print :: Show a => a -> IO ()
- getChar :: IO Char
- getLine :: IO String
- getContents :: IO String
- readFile :: FilePath -> IO String
- writeFile :: FilePath -> String -> IO ()
- appendFile :: FilePath -> String -> IO ()
- readIO :: Read a => String -> IO a
- readLn :: Read a => IO a
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.
data BufferMode Source
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.hFlushis issued, or the handle is closed. - block-buffering: the entire buffer is written out whenever it
overflows, a
System.IO.hFlushis 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.hLookAheadoperation 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 |
hReady :: Handle -> IO BoolSource
Computation hReady hdl indicates whether at least one item is
available for input from handle hdl.
This operation may fail with:
-
System.IO.Error.isEOFErrorif the end of file has been reached.
hPrint :: Show a => Handle -> a -> IO ()Source
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.isFullErrorif the device is full; or -
System.IO.Error.isPermissionErrorif another system resource limit would be exceeded.
isAlreadyExistsError :: IOError -> BoolSource
An error indicating that an IO operation failed because
one of its arguments already exists.
isDoesNotExistError :: IOError -> BoolSource
An error indicating that an IO operation failed because
one of its arguments does not exist.
isAlreadyInUseError :: IOError -> BoolSource
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).
isFullError :: IOError -> BoolSource
An error indicating that an IO operation failed because
the device is full.
isEOFError :: IOError -> BoolSource
An error indicating that an IO operation failed because
the end of file has been reached.
isIllegalOperation :: IOError -> BoolSource
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.
isPermissionError :: IOError -> BoolSource
An error indicating that an IO operation failed because
the user does not have sufficient operating system privilege
to perform that operation.
isUserError :: IOError -> BoolSource
A programmer-defined error value constructed using userError.
ioeGetHandle :: IOError -> Maybe HandleSource
try :: IO a -> IO (Either IOError a)Source
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.
bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
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.
bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
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.
data IO a
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.
type IOError = IOExceptionSource
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.
catch :: IO a -> (IOError -> IO a) -> IO aSource
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.
interact :: (String -> String) -> IO ()Source
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.
print :: Show a => a -> IO ()Source
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]])
getContents :: IO StringSource
The getContents operation returns all user input as a single string,
which is read lazily as it is needed
(same as hGetContents stdin).
readFile :: FilePath -> IO StringSource
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.
writeFile :: FilePath -> String -> IO ()Source
The computation writeFile file str function writes the string str,
to the file file.
appendFile :: FilePath -> String -> IO ()Source
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]])