| ||||||||||||
| ||||||||||||
| ||||||||||||
Description | ||||||||||||
The standard IO library. | ||||||||||||
Synopsis | ||||||||||||
The IO monad | ||||||||||||
data IO a | ||||||||||||
| ||||||||||||
fixIO :: (a -> IO a) -> IO a | ||||||||||||
Files and handles | ||||||||||||
type FilePath = String | ||||||||||||
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. | ||||||||||||
data Handle | ||||||||||||
| ||||||||||||
Standard handles | ||||||||||||
Three handles are allocated during program initialisation, and are initially open. | ||||||||||||
stdin :: Handle | ||||||||||||
A handle managing input from the Haskell program's standard input channel. | ||||||||||||
stdout :: Handle | ||||||||||||
A handle managing output to the Haskell program's standard output channel. | ||||||||||||
stderr :: Handle | ||||||||||||
A handle managing output to the Haskell program's standard error channel. | ||||||||||||
Opening and closing files | ||||||||||||
Opening files | ||||||||||||
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r | ||||||||||||
withFile name mode act opens a file using openFile and passes the resulting handle to the computation act. The handle will be closed on exit from withFile, whether by normal termination or by raising an exception. | ||||||||||||
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. 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:
Note: if you will be working with files containing binary data, you'll want to be using openBinaryFile. | ||||||||||||
data IOMode | ||||||||||||
| ||||||||||||
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. Performing hClose on a handle that has already been closed has no effect; doing so 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. | ||||||||||||
Special cases | ||||||||||||
These functions are also exported by the Prelude. | ||||||||||||
readFile :: FilePath -> IO String | ||||||||||||
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 () | ||||||||||||
The computation writeFile file str function writes the string str, to the file file. | ||||||||||||
appendFile :: FilePath -> String -> IO () | ||||||||||||
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]]) | ||||||||||||
File locking | ||||||||||||
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 isAlreadyInUseError. | ||||||||||||
Operations on handles | ||||||||||||
Determining and changing 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. | ||||||||||||
hSetFileSize :: Handle -> Integer -> IO () | ||||||||||||
hSetFileSize hdl size truncates the physical file with handle hdl to size bytes. | ||||||||||||
Detecting the end of input | ||||||||||||
hIsEOF :: Handle -> IO Bool | ||||||||||||
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. | ||||||||||||
isEOF :: IO Bool | ||||||||||||
The computation isEOF is identical to hIsEOF, except that it works only on stdin. | ||||||||||||
Buffering operations | ||||||||||||
data BufferMode | ||||||||||||
| ||||||||||||
hSetBuffering :: Handle -> BufferMode -> IO () | ||||||||||||
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
This operation may fail with:
| ||||||||||||
hGetBuffering :: Handle -> IO BufferMode | ||||||||||||
Computation hGetBuffering hdl returns the current buffering mode for hdl. | ||||||||||||
hFlush :: Handle -> IO () | ||||||||||||
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:
| ||||||||||||
Repositioning handles | ||||||||||||
hGetPosn :: Handle -> IO HandlePosn | ||||||||||||
Computation hGetPosn hdl returns the current I/O position of hdl as a value of the abstract type HandlePosn. | ||||||||||||
hSetPosn :: HandlePosn -> IO () | ||||||||||||
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:
| ||||||||||||
data HandlePosn | ||||||||||||
| ||||||||||||
hSeek :: Handle -> SeekMode -> Integer -> IO () | ||||||||||||
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:
| ||||||||||||
data SeekMode | ||||||||||||
| ||||||||||||
hTell :: Handle -> IO Integer | ||||||||||||
Handle properties | ||||||||||||
hIsOpen :: Handle -> IO Bool | ||||||||||||
hIsClosed :: Handle -> IO Bool | ||||||||||||
hIsReadable :: Handle -> IO Bool | ||||||||||||
hIsWritable :: Handle -> IO Bool | ||||||||||||
hIsSeekable :: Handle -> IO Bool | ||||||||||||
Terminal operations (not portable: GHC/Hugs only) | ||||||||||||
hIsTerminalDevice :: Handle -> IO Bool | ||||||||||||
Is the handle connected to a terminal? | ||||||||||||
hSetEcho :: Handle -> Bool -> IO () | ||||||||||||
Set the echoing status of a handle connected to a terminal. | ||||||||||||
hGetEcho :: Handle -> IO Bool | ||||||||||||
Get the echoing status of a handle connected to a terminal. | ||||||||||||
Showing handle state (not portable: GHC only) | ||||||||||||
hShow :: Handle -> IO String | ||||||||||||
hShow is in the IO monad, and gives more comprehensive output than the (pure) instance of Show for Handle. | ||||||||||||
Text input and output | ||||||||||||
Text input | ||||||||||||
hWaitForInput :: Handle -> Int -> IO Bool | ||||||||||||
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. | ||||||||||||
hReady :: Handle -> IO Bool | ||||||||||||
Computation hReady hdl indicates whether at least one item is available for input from handle hdl. This operation may fail with:
| ||||||||||||
hGetChar :: Handle -> IO Char | ||||||||||||
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:
| ||||||||||||
hGetLine :: Handle -> IO String | ||||||||||||
Computation hGetLine hdl reads a line from the file or channel managed by hdl. 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 | ||||||||||||
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:
| ||||||||||||
hGetContents :: Handle -> IO String | ||||||||||||
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:
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:
| ||||||||||||
Text output | ||||||||||||
hPutChar :: Handle -> Char -> IO () | ||||||||||||
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:
| ||||||||||||
hPutStr :: Handle -> String -> IO () | ||||||||||||
Computation hPutStr hdl s writes the string s to the file or channel managed by hdl. This operation may fail with:
| ||||||||||||
hPutStrLn :: Handle -> String -> IO () | ||||||||||||
The same as hPutStr, but adds a newline character. | ||||||||||||
hPrint :: Show a => Handle -> a -> IO () | ||||||||||||
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:
| ||||||||||||
Special cases for standard input and output | ||||||||||||
These functions are also exported by the Prelude. | ||||||||||||
interact :: (String -> String) -> IO () | ||||||||||||
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. | ||||||||||||
putChar :: Char -> IO () | ||||||||||||
Write a character to the standard output device (same as hPutChar stdout). | ||||||||||||
putStr :: String -> IO () | ||||||||||||
Write a string to the standard output device (same as hPutStr stdout). | ||||||||||||
putStrLn :: String -> IO () | ||||||||||||
The same as putStr, but adds a newline character. | ||||||||||||
print :: Show a => a -> IO () | ||||||||||||
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]]) | ||||||||||||
getChar :: IO Char | ||||||||||||
Read a character from the standard input device (same as hGetChar stdin). | ||||||||||||
getLine :: IO String | ||||||||||||
Read a line from the standard input device (same as hGetLine stdin). | ||||||||||||
getContents :: IO String | ||||||||||||
The getContents operation returns all user input as a single string, which is read lazily as it is needed (same as hGetContents stdin). | ||||||||||||
readIO :: Read a => String -> IO a | ||||||||||||
The readIO function is similar to read except that it signals parse failure to the IO monad instead of terminating the program. | ||||||||||||
readLn :: Read a => IO a | ||||||||||||
The readLn function combines getLine and readIO. | ||||||||||||
Binary input and output | ||||||||||||
withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r | ||||||||||||
withBinaryFile name mode act opens a file using openBinaryFile and passes the resulting handle to the computation act. The handle will be closed on exit from withBinaryFile, whether by normal termination or by raising an exception. | ||||||||||||
openBinaryFile :: FilePath -> IOMode -> IO Handle | ||||||||||||
Like openFile, but open the file in binary mode. On Windows, reading a file in text mode (which is the default) will translate CRLF to LF, and writing will translate LF to CRLF. This is usually what you want with text files. With binary files this is undesirable; also, as usual under Microsoft operating systems, text mode treats control-Z as EOF. Binary mode turns off all special treatment of end-of-line and end-of-file characters. (See also hSetBinaryMode.) | ||||||||||||
hSetBinaryMode :: Handle -> Bool -> IO () | ||||||||||||
Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.) | ||||||||||||
hPutBuf :: Handle -> Ptr a -> Int -> IO () | ||||||||||||
hPutBuf hdl buf count writes count 8-bit bytes from the buffer buf to the handle hdl. It returns (). This operation may fail with:
| ||||||||||||
hGetBuf :: Handle -> Ptr a -> Int -> IO Int | ||||||||||||
hGetBuf hdl buf count reads data from the handle hdl into the buffer buf until either EOF is reached or count 8-bit bytes have been read. It returns the number of bytes actually read. This may be zero if EOF was reached before any data was read (or if count is zero). hGetBuf never raises an EOF exception, instead it returns a value smaller than count. If the handle is a pipe or socket, and the writing end is closed, hGetBuf will behave as if EOF was reached. | ||||||||||||
hPutBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int | ||||||||||||
hGetBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int | ||||||||||||
hGetBufNonBlocking hdl buf count reads data from the handle hdl into the buffer buf until either EOF is reached, or count 8-bit bytes have been read, or there is no more data available to read immediately. hGetBufNonBlocking is identical to hGetBuf, except that it will never block waiting for data to become available, instead it returns only whatever data is available. To wait for data to arrive before calling hGetBufNonBlocking, use hWaitForInput. If the handle is a pipe or socket, and the writing end is closed, hGetBufNonBlocking will behave as if EOF was reached. | ||||||||||||
Temporary files (not portable: GHC/Hugs only) | ||||||||||||
openTempFile | ||||||||||||
| ||||||||||||
openBinaryTempFile :: FilePath -> String -> IO (FilePath, Handle) | ||||||||||||
Like openTempFile, but opens the file in binary mode. See openBinaryFile for more comments. | ||||||||||||
Produced by Haddock version 0.8 |