data IO 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.
instance Monad IO |
instance Functor IO |
type FilePath = String |
data Handle |
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.
instance Eq Handle |
instance Show Handle |
Three handles are allocated during program initialisation, and are initially open.
stdin :: Handle |
stdout :: Handle |
stderr :: Handle |
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r |
openFile :: FilePath -> IOMode -> IO Handle |
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 modeWriteMode 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:
data IOMode |
= | ReadMode | |
| | WriteMode | |
| | AppendMode | |
| | ReadWriteMode | |
See System.IO.openFile
instance Enum IOMode |
instance Eq IOMode |
instance Ord IOMode |
instance Read IOMode |
instance Show IOMode |
instance Ix IOMode |
hClose :: Handle -> IO () |
These functions are also exported by the Prelude.
readFile :: FilePath -> IO String |
writeFile :: FilePath -> String -> IO () |
appendFile :: FilePath -> String -> IO () |
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.
Implementations should enforce as far as possible, at least locally to the Haskell process, multiple-reader single-writer locking on files. That is, 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. Whether two files are the same is implementation-dependent, but they should normally be the same if they have the same absolute path name and neither has been renamed, for example.
Warning: the readFile operation holds a semi-closed handle on the file until the entire contents of the file have been consumed. It follows that an attempt to write to a file (using writeFile, for example) that was earlier opened by readFile will usually result in failure with System.IO.Error.isAlreadyInUseError.
hFileSize :: Handle -> IO Integer |
hSetFileSize :: Handle -> Integer -> IO () |
hIsEOF :: Handle -> IO Bool |
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.
isEOF :: IO Bool |
data BufferMode |
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:
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:
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.
instance Eq BufferMode |
instance Ord BufferMode |
instance Read BufferMode |
instance Show BufferMode |
hSetBuffering :: Handle -> BufferMode -> IO () |
If the buffer mode is changed from BlockBuffering or LineBuffering to NoBuffering, then
This operation may fail with:
hGetBuffering :: Handle -> IO BufferMode |
hFlush :: Handle -> IO () |
This operation may fail with:
hGetPosn :: Handle -> IO HandlePosn |
hSetPosn :: HandlePosn -> IO () |
This operation may fail with:
data HandlePosn |
instance Eq HandlePosn |
instance Show HandlePosn |
hSeek :: Handle -> SeekMode -> Integer -> IO () |
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:
data SeekMode |
= | 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. |
A mode that determines the effect of hSeekhdl mode i.
instance Enum SeekMode |
instance Eq SeekMode |
instance Ord SeekMode |
instance Read SeekMode |
instance Show SeekMode |
instance Ix SeekMode |
hTell :: Handle -> IO Integer |
This operation may fail with:
Each of these operations returns True if the handle has the the specified property, or False otherwise.
hIsTerminalDevice :: Handle -> IO Bool |
hSetEcho :: Handle -> Bool -> IO () |
hGetEcho :: Handle -> IO Bool |
hShow :: Handle -> IO String |
hWaitForInput :: Handle -> Int -> IO Bool |
If t is less than zero, then hWaitForInput waits indefinitely.
This operation may fail with:
hReady :: Handle -> IO Bool |
This operation may fail with:
hGetChar :: Handle -> IO Char |
This operation may fail with:
hGetLine :: Handle -> IO String |
This operation may fail with:
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.
hLookAhead :: Handle -> IO Char |
This operation may fail with:
hGetContents :: Handle -> IO String |
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:
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:
hPutChar :: Handle -> Char -> IO () |
This operation may fail with:
hPutStr :: Handle -> String -> IO () |
This operation may fail with:
hPutStrLn :: Handle -> String -> IO () |
hPrint :: Show a => Handle -> a -> IO () |
This operation may fail with:
These functions are also exported by the Prelude.
interact :: (String -> String) -> IO () |
putChar :: Char -> IO () |
putStr :: String -> IO () |
putStrLn :: String -> IO () |
print :: Show a => a -> IO () |
For example, a program to print the first 20 integers and their powers of 2 could be written as:
getChar :: IO Char |
getLine :: IO String |
getContents :: IO String |
readIO :: Read a => String -> IO a |
readLn :: Read a => IO a |