<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>System.IO</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ window.onload = function () {pageLoad();setSynopsis("mini_System-IO.html");}; //]]> </script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">haskell2010-1.1.0.1: Compatibility with Haskell 2010</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Safe Haskell</th><td>Safe</td></tr></table><p class="caption">System.IO</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">The IO monad </a></li><li><a href="#g:2">Files and handles </a><ul><li><a href="#g:3">Standard handles </a></li></ul></li><li><a href="#g:4">Opening and closing files </a><ul><li><a href="#g:5">Opening files </a></li><li><a href="#g:6">Closing files </a></li><li><a href="#g:7">Special cases </a></li><li><a href="#g:8">File locking </a></li></ul></li><li><a href="#g:9">Operations on handles </a><ul><li><a href="#g:10">Determining and changing the size of a file </a></li><li><a href="#g:11">Detecting the end of input </a></li><li><a href="#g:12">Buffering operations </a></li><li><a href="#g:13">Repositioning handles </a></li><li><a href="#g:14">Handle properties </a></li><li><a href="#g:15">Terminal operations </a></li><li><a href="#g:16">Showing handle state </a></li></ul></li><li><a href="#g:17">Text input and output </a><ul><li><a href="#g:18">Text input </a></li><li><a href="#g:19">Text output </a></li><li><a href="#g:20">Special cases for standard input and output </a></li></ul></li></ul></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">data</span> <a href="#t:IO">IO</a> a</li><li class="src short"><a href="#v:fixIO">fixIO</a> :: (a -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><span class="keyword">type</span> <a href="#t:FilePath">FilePath</a> = <a href="Prelude.html#t:String">String</a></li><li class="src short"><span class="keyword">data</span> <a href="#t:Handle">Handle</a> </li><li class="src short"><a href="#v:stdin">stdin</a> :: <a href="System-IO.html#t:Handle">Handle</a></li><li class="src short"><a href="#v:stdout">stdout</a> :: <a href="System-IO.html#t:Handle">Handle</a></li><li class="src short"><a href="#v:stderr">stderr</a> :: <a href="System-IO.html#t:Handle">Handle</a></li><li class="src short"><a href="#v:withFile">withFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="System-IO.html#t:IOMode">IOMode</a> -> (<a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> r) -> <a href="System-IO.html#t:IO">IO</a> r</li><li class="src short"><a href="#v:openFile">openFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="System-IO.html#t:IOMode">IOMode</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="System-IO.html#t:Handle">Handle</a></li><li class="src short"><span class="keyword">data</span> <a href="#t:IOMode">IOMode</a> <ul class="subs"><li>= <a href="#v:ReadMode">ReadMode</a> </li><li>| <a href="#v:WriteMode">WriteMode</a> </li><li>| <a href="#v:AppendMode">AppendMode</a> </li><li>| <a href="#v:ReadWriteMode">ReadWriteMode</a> </li></ul></li><li class="src short"><a href="#v:hClose">hClose</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:readFile">readFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a></li><li class="src short"><a href="#v:writeFile">writeFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:appendFile">appendFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:hFileSize">hFileSize</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Integer">Integer</a></li><li class="src short"><a href="#v:hSetFileSize">hSetFileSize</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:Integer">Integer</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:hIsEOF">hIsEOF</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:isEOF">isEOF</a> :: <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><span class="keyword">data</span> <a href="#t:BufferMode">BufferMode</a> <ul class="subs"><li>= <a href="#v:NoBuffering">NoBuffering</a> </li><li>| <a href="#v:LineBuffering">LineBuffering</a> </li><li>| <a href="#v:BlockBuffering">BlockBuffering</a> (<a href="Prelude.html#t:Maybe">Maybe</a> <a href="Prelude.html#t:Int">Int</a>) </li></ul></li><li class="src short"><a href="#v:hSetBuffering">hSetBuffering</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:BufferMode">BufferMode</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:hGetBuffering">hGetBuffering</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="System-IO.html#t:BufferMode">BufferMode</a></li><li class="src short"><a href="#v:hFlush">hFlush</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:hGetPosn">hGetPosn</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="System-IO.html#t:HandlePosn">HandlePosn</a></li><li class="src short"><a href="#v:hSetPosn">hSetPosn</a> :: <a href="System-IO.html#t:HandlePosn">HandlePosn</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><span class="keyword">data</span> <a href="#t:HandlePosn">HandlePosn</a> </li><li class="src short"><a href="#v:hSeek">hSeek</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:SeekMode">SeekMode</a> -> <a href="Prelude.html#t:Integer">Integer</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><span class="keyword">data</span> <a href="#t:SeekMode">SeekMode</a> <ul class="subs"><li>= <a href="#v:AbsoluteSeek">AbsoluteSeek</a> </li><li>| <a href="#v:RelativeSeek">RelativeSeek</a> </li><li>| <a href="#v:SeekFromEnd">SeekFromEnd</a> </li></ul></li><li class="src short"><a href="#v:hTell">hTell</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Integer">Integer</a></li><li class="src short"><a href="#v:hIsOpen">hIsOpen</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hIsClosed">hIsClosed</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hIsReadable">hIsReadable</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hIsWritable">hIsWritable</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hIsSeekable">hIsSeekable</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hIsTerminalDevice">hIsTerminalDevice</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hSetEcho">hSetEcho</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:Bool">Bool</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:hGetEcho">hGetEcho</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hShow">hShow</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a></li><li class="src short"><a href="#v:hWaitForInput">hWaitForInput</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:Int">Int</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hReady">hReady</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:hGetChar">hGetChar</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Char">Char</a></li><li class="src short"><a href="#v:hGetLine">hGetLine</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a></li><li class="src short"><a href="#v:hLookAhead">hLookAhead</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Char">Char</a></li><li class="src short"><a href="#v:hGetContents">hGetContents</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a></li><li class="src short"><a href="#v:hPutChar">hPutChar</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:Char">Char</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:hPutStr">hPutStr</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:hPutStrLn">hPutStrLn</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:hPrint">hPrint</a> :: <a href="Prelude.html#t:Show">Show</a> a => <a href="System-IO.html#t:Handle">Handle</a> -> a -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:interact">interact</a> :: (<a href="Prelude.html#t:String">String</a> -> <a href="Prelude.html#t:String">String</a>) -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:putChar">putChar</a> :: <a href="Prelude.html#t:Char">Char</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:putStr">putStr</a> :: <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:putStrLn">putStrLn</a> :: <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:print">print</a> :: <a href="Prelude.html#t:Show">Show</a> a => a -> <a href="System-IO.html#t:IO">IO</a> ()</li><li class="src short"><a href="#v:getChar">getChar</a> :: <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Char">Char</a></li><li class="src short"><a href="#v:getLine">getLine</a> :: <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a></li><li class="src short"><a href="#v:getContents">getContents</a> :: <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a></li><li class="src short"><a href="#v:readIO">readIO</a> :: <a href="Prelude.html#t:Read">Read</a> a => <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:readLn">readLn</a> :: <a href="Prelude.html#t:Read">Read</a> a => <a href="System-IO.html#t:IO">IO</a> a</li></ul></div><div id="interface"><h1 id="g:1">The IO monad </h1><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:IO" class="def">IO</a> a <a href="../ghc-prim-0.2.0.0/src/GHC-Types.html#IO" class="link">Source</a></p><div class="doc"><p>A value of type <code><code><a href="System-IO.html#t:IO">IO</a></code> a</code> is a computation which, when performed, does some I/O before returning a value of type <code>a</code>. </p><p>There is really only one way to "perform" an I/O action: bind it to <code>Main.main</code> 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 <code><a href="System-IO.html#t:IO">IO</a></code> monad and called at some point, directly or indirectly, from <code>Main.main</code>. </p><p><code><a href="System-IO.html#t:IO">IO</a></code> is a monad, so <code><a href="System-IO.html#t:IO">IO</a></code> actions can be combined using either the do-notation or the <code>>></code> and <code>>>=</code> operations from the <code>Monad</code> class. </p></div><div class="subs instances"><p id="control.i:IO" class="caption collapser" onclick="toggleSection('i:IO')">Instances</p><div id="section.i:IO" class="show"><table><tr><td class="src"><a href="Prelude.html#t:Monad">Monad</a> <a href="System-IO.html#t:IO">IO</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Functor">Functor</a> <a href="System-IO.html#t:IO">IO</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="../base-4.5.1.0/Data-Typeable-Internal.html#t:Typeable1">Typeable1</a> <a href="System-IO.html#t:IO">IO</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:fixIO" class="def">fixIO</a> :: (a -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a<a href="../base-4.5.1.0/src/System-IO.html#fixIO" class="link">Source</a></p></div><h1 id="g:2">Files and handles </h1><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:FilePath" class="def">FilePath</a> = <a href="Prelude.html#t:String">String</a><a href="../base-4.5.1.0/src/GHC-IO.html#FilePath" class="link">Source</a></p><div class="doc"><p>File and directory names are values of type <code><a href="Prelude.html#t:String">String</a></code>, 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. </p></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:Handle" class="def">Handle</a> <a href="../base-4.5.1.0/src/GHC-IO-Handle-Types.html#Handle" class="link">Source</a></p><div class="doc"><p>Haskell defines operations to read and write characters from and to files, represented by values of type <code>Handle</code>. Each value of this type is a <em>handle</em>: a record used by the Haskell run-time system to <em>manage</em> I/O with file system objects. A handle has at least the following properties: </p><ul><li> whether it manages input or output or both; </li><li> whether it is <em>open</em>, <em>closed</em> or <em>semi-closed</em>; </li><li> whether the object is seekable; </li><li> whether buffering is disabled, or enabled on a line or block basis; </li><li> a buffer (whose length may be zero). </li></ul><p>Most handles will also have a current I/O position indicating where the next input or output operation will occur. A handle is <em>readable</em> if it manages only input or both input and output; likewise, it is <em>writable</em> if it manages only output or both input and output. A handle is <em>open</em> 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 <code><a href="Prelude.html#t:Show">Show</a></code> and <code><a href="Prelude.html#t:Eq">Eq</a></code> 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 <code><a href="Prelude.html#v:-61--61-">==</a></code> only to itself; no attempt is made to compare the internal state of different handles for equality. </p></div><div class="subs instances"><p id="control.i:Handle" class="caption collapser" onclick="toggleSection('i:Handle')">Instances</p><div id="section.i:Handle" class="show"><table><tr><td class="src"><a href="Prelude.html#t:Eq">Eq</a> <a href="System-IO.html#t:Handle">Handle</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Show">Show</a> <a href="System-IO.html#t:Handle">Handle</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="../base-4.5.1.0/Data-Typeable-Internal.html#t:Typeable">Typeable</a> <a href="System-IO.html#t:Handle">Handle</a></td><td class="doc empty"> </td></tr></table></div></div></div><h2 id="g:3">Standard handles </h2><div class="doc"><p>Three handles are allocated during program initialisation, and are initially open. </p></div><div class="top"><p class="src"><a name="v:stdin" class="def">stdin</a> :: <a href="System-IO.html#t:Handle">Handle</a><a href="../base-4.5.1.0/src/GHC-IO-Handle-FD.html#stdin" class="link">Source</a></p><div class="doc"><p>A handle managing input from the Haskell program's standard input channel. </p></div></div><div class="top"><p class="src"><a name="v:stdout" class="def">stdout</a> :: <a href="System-IO.html#t:Handle">Handle</a><a href="../base-4.5.1.0/src/GHC-IO-Handle-FD.html#stdout" class="link">Source</a></p><div class="doc"><p>A handle managing output to the Haskell program's standard output channel. </p></div></div><div class="top"><p class="src"><a name="v:stderr" class="def">stderr</a> :: <a href="System-IO.html#t:Handle">Handle</a><a href="../base-4.5.1.0/src/GHC-IO-Handle-FD.html#stderr" class="link">Source</a></p><div class="doc"><p>A handle managing output to the Haskell program's standard error channel. </p></div></div><h1 id="g:4">Opening and closing files </h1><h2 id="g:5">Opening files </h2><div class="top"><p class="src"><a name="v:withFile" class="def">withFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="System-IO.html#t:IOMode">IOMode</a> -> (<a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> r) -> <a href="System-IO.html#t:IO">IO</a> r<a href="../base-4.5.1.0/src/System-IO.html#withFile" class="link">Source</a></p><div class="doc"><p><code><code><a href="System-IO.html#v:withFile">withFile</a></code> name mode act</code> opens a file using <code><a href="../base-4.5.1.0/GHC-IO-Handle-FD.html#v:openFile">openFile</a></code> and passes the resulting handle to the computation <code>act</code>. The handle will be closed on exit from <code><a href="System-IO.html#v:withFile">withFile</a></code>, whether by normal termination or by raising an exception. If closing the handle raises an exception, then this exception will be raised by <code><a href="System-IO.html#v:withFile">withFile</a></code> rather than any exception raised by <code>act</code>. </p></div></div><div class="top"><p class="src"><a name="v:openFile" class="def">openFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="System-IO.html#t:IOMode">IOMode</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="System-IO.html#t:Handle">Handle</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:openFile">openFile</a></code> <code>file mode</code> allocates and returns a new, open handle to manage the file <code>file</code>. It manages input if <code>mode</code> is <code><a href="System-IO.html#v:ReadMode">ReadMode</a></code>, output if <code>mode</code> is <code><a href="System-IO.html#v:WriteMode">WriteMode</a></code> or <code><a href="System-IO.html#v:AppendMode">AppendMode</a></code>, and both input and output if mode is <code><a href="System-IO.html#v:ReadWriteMode">ReadWriteMode</a></code>. </p><p>If the file does not exist and it is opened for output, it should be created as a new file. If <code>mode</code> is <code><a href="System-IO.html#v:WriteMode">WriteMode</a></code> 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 <code><a href="System-IO.html#v:openFile">openFile</a></code> with <code>mode</code> <code><a href="System-IO.html#v:WriteMode">WriteMode</a></code> unless it is subsequently written to successfully. The handle is positioned at the end of the file if <code>mode</code> is <code><a href="System-IO.html#v:AppendMode">AppendMode</a></code>, and otherwise at the beginning (in which case its internal position is 0). The initial buffer mode is implementation-dependent. </p><p>This operation may fail with: </p><ul><li> <code>isAlreadyInUseError</code> if the file is already open and cannot be reopened; </li><li> <code>isDoesNotExistError</code> if the file does not exist; or </li><li> <code>isPermissionError</code> if the user does not have permission to open the file. </li></ul></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:IOMode" class="def">IOMode</a> <a href="../base-4.5.1.0/src/GHC-IO-IOMode.html#IOMode" class="link">Source</a></p><div class="doc"><p>See <code><a href="../base-4.5.1.0/System-IO.html#v:openFile">openFile</a></code> </p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:ReadMode" class="def">ReadMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a name="v:WriteMode" class="def">WriteMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a name="v:AppendMode" class="def">AppendMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a name="v:ReadWriteMode" class="def">ReadWriteMode</a></td><td class="doc empty"> </td></tr></table></div><div class="subs instances"><p id="control.i:IOMode" class="caption collapser" onclick="toggleSection('i:IOMode')">Instances</p><div id="section.i:IOMode" class="show"><table><tr><td class="src"><a href="Prelude.html#t:Enum">Enum</a> <a href="System-IO.html#t:IOMode">IOMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Eq">Eq</a> <a href="System-IO.html#t:IOMode">IOMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Ord">Ord</a> <a href="System-IO.html#t:IOMode">IOMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Read">Read</a> <a href="System-IO.html#t:IOMode">IOMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Show">Show</a> <a href="System-IO.html#t:IOMode">IOMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Ix.html#t:Ix">Ix</a> <a href="System-IO.html#t:IOMode">IOMode</a></td><td class="doc empty"> </td></tr></table></div></div></div><h2 id="g:6">Closing files </h2><div class="top"><p class="src"><a name="v:hClose" class="def">hClose</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hClose" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hClose">hClose</a></code> <code>hdl</code> makes handle <code>hdl</code> closed. Before the computation finishes, if <code>hdl</code> is writable its buffer is flushed as for <code><a href="System-IO.html#v:hFlush">hFlush</a></code>. Performing <code><a href="System-IO.html#v:hClose">hClose</a></code> 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 <code><a href="System-IO.html#v:hClose">hClose</a></code> fails for any reason, any further operations (apart from <code><a href="System-IO.html#v:hClose">hClose</a></code>) on the handle will still fail as if <code>hdl</code> had been successfully closed. </p></div></div><h2 id="g:7">Special cases </h2><div class="doc"><p>These functions are also exported by the <a href="../base-4.5.1.0/Prelude.html">Prelude</a>. </p></div><div class="top"><p class="src"><a name="v:readFile" class="def">readFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a><a href="../base-4.5.1.0/src/System-IO.html#readFile" class="link">Source</a></p><div class="doc"><p>The <code><a href="System-IO.html#v:readFile">readFile</a></code> function reads a file and returns the contents of the file as a string. The file is read lazily, on demand, as with <code><a href="System-IO.html#v:getContents">getContents</a></code>. </p></div></div><div class="top"><p class="src"><a name="v:writeFile" class="def">writeFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/System-IO.html#writeFile" class="link">Source</a></p><div class="doc"><p>The computation <code><a href="System-IO.html#v:writeFile">writeFile</a></code> <code>file str</code> function writes the string <code>str</code>, to the file <code>file</code>. </p></div></div><div class="top"><p class="src"><a name="v:appendFile" class="def">appendFile</a> :: <a href="System-IO.html#t:FilePath">FilePath</a> -> <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/System-IO.html#appendFile" class="link">Source</a></p><div class="doc"><p>The computation <code><a href="System-IO.html#v:appendFile">appendFile</a></code> <code>file str</code> function appends the string <code>str</code>, to the file <code>file</code>. </p><p>Note that <code><a href="System-IO.html#v:writeFile">writeFile</a></code> and <code><a href="System-IO.html#v:appendFile">appendFile</a></code> write a literal string to a file. To write a value of any printable type, as with <code><a href="System-IO.html#v:print">print</a></code>, use the <code><a href="Prelude.html#v:show">show</a></code> function to convert the value to a string first. </p><pre> main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]]) </pre></div></div><h2 id="g:8">File locking </h2><div class="doc"><p>Implementations should enforce as far as possible, at least locally to the Haskell process, multiple-reader single-writer locking on files. That is, <em>there may either be many handles on the same file which manage input, or just one handle on the file which manages output</em>. 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. </p><p><em>Warning</em>: the <code><a href="System-IO.html#v:readFile">readFile</a></code> 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 <code><a href="System-IO.html#v:writeFile">writeFile</a></code>, for example) that was earlier opened by <code><a href="System-IO.html#v:readFile">readFile</a></code> will usually result in failure with <code><a href="../base-4.5.1.0/System-IO-Error.html#v:isAlreadyInUseError">isAlreadyInUseError</a></code>. </p></div><h1 id="g:9">Operations on handles </h1><h2 id="g:10">Determining and changing the size of a file </h2><div class="top"><p class="src"><a name="v:hFileSize" class="def">hFileSize</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Integer">Integer</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hFileSize" class="link">Source</a></p><div class="doc"><p>For a handle <code>hdl</code> which attached to a physical file, <code><a href="System-IO.html#v:hFileSize">hFileSize</a></code> <code>hdl</code> returns the size of that file in 8-bit bytes. </p></div></div><div class="top"><p class="src"><a name="v:hSetFileSize" class="def">hSetFileSize</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:Integer">Integer</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hSetFileSize" class="link">Source</a></p><div class="doc"><p><code><a href="System-IO.html#v:hSetFileSize">hSetFileSize</a></code> <code>hdl</code> <code>size</code> truncates the physical file with handle <code>hdl</code> to <code>size</code> bytes. </p></div></div><h2 id="g:11">Detecting the end of input </h2><div class="top"><p class="src"><a name="v:hIsEOF" class="def">hIsEOF</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hIsEOF" class="link">Source</a></p><div class="doc"><p>For a readable handle <code>hdl</code>, <code><a href="System-IO.html#v:hIsEOF">hIsEOF</a></code> <code>hdl</code> returns <code><a href="Prelude.html#v:True">True</a></code> if no further input can be taken from <code>hdl</code> or for a physical file, if the current I/O position is equal to the length of the file. Otherwise, it returns <code><a href="Prelude.html#v:False">False</a></code>. </p><p>NOTE: <code><a href="System-IO.html#v:hIsEOF">hIsEOF</a></code> may block, because it has to attempt to read from the stream to determine whether there is any more data to be read. </p></div></div><div class="top"><p class="src"><a name="v:isEOF" class="def">isEOF</a> :: <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle-FD.html#isEOF" class="link">Source</a></p><div class="doc"><p>The computation <code><a href="System-IO.html#v:isEOF">isEOF</a></code> is identical to <code><a href="System-IO.html#v:hIsEOF">hIsEOF</a></code>, except that it works only on <code><a href="System-IO.html#v:stdin">stdin</a></code>. </p></div></div><h2 id="g:12">Buffering operations </h2><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:BufferMode" class="def">BufferMode</a> <a href="../base-4.5.1.0/src/GHC-IO-Handle-Types.html#BufferMode" class="link">Source</a></p><div class="doc"><p>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 <em>flushed</em>, from the internal buffer according to the buffer mode: </p><ul><li> <em>line-buffering</em>: the entire output buffer is flushed whenever a newline is output, the buffer overflows, a <code><a href="../base-4.5.1.0/System-IO.html#v:hFlush">hFlush</a></code> is issued, or the handle is closed. </li><li> <em>block-buffering</em>: the entire buffer is written out whenever it overflows, a <code><a href="../base-4.5.1.0/System-IO.html#v:hFlush">hFlush</a></code> is issued, or the handle is closed. </li><li> <em>no-buffering</em>: output is written immediately, and never stored in the buffer. </li></ul><p>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. </p><p>Similarly, input occurs according to the buffer mode for the handle: </p><ul><li> <em>line-buffering</em>: 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. </li><li> <em>block-buffering</em>: when the buffer for the handle becomes empty, the next block of data is read into the buffer. </li><li> <em>no-buffering</em>: the next input item is read and returned. The <code><a href="../base-4.5.1.0/System-IO.html#v:hLookAhead">hLookAhead</a></code> operation implies that even a no-buffered handle may require a one-character buffer. </li></ul><p>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. </p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:NoBuffering" class="def">NoBuffering</a></td><td class="doc"><p>buffering is disabled if possible. </p></td></tr><tr><td class="src"><a name="v:LineBuffering" class="def">LineBuffering</a></td><td class="doc"><p>line-buffering should be enabled if possible. </p></td></tr><tr><td class="src"><a name="v:BlockBuffering" class="def">BlockBuffering</a> (<a href="Prelude.html#t:Maybe">Maybe</a> <a href="Prelude.html#t:Int">Int</a>)</td><td class="doc"><p>block-buffering should be enabled if possible. The size of the buffer is <code>n</code> items if the argument is <code><a href="Prelude.html#v:Just">Just</a></code> <code>n</code> and is otherwise implementation-dependent. </p></td></tr></table></div><div class="subs instances"><p id="control.i:BufferMode" class="caption collapser" onclick="toggleSection('i:BufferMode')">Instances</p><div id="section.i:BufferMode" class="show"><table><tr><td class="src"><a href="Prelude.html#t:Eq">Eq</a> <a href="System-IO.html#t:BufferMode">BufferMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Ord">Ord</a> <a href="System-IO.html#t:BufferMode">BufferMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Read">Read</a> <a href="System-IO.html#t:BufferMode">BufferMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Show">Show</a> <a href="System-IO.html#t:BufferMode">BufferMode</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:hSetBuffering" class="def">hSetBuffering</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:BufferMode">BufferMode</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hSetBuffering" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hSetBuffering">hSetBuffering</a></code> <code>hdl mode</code> sets the mode of buffering for handle <code>hdl</code> on subsequent reads and writes. </p><p>If the buffer mode is changed from <code><a href="System-IO.html#v:BlockBuffering">BlockBuffering</a></code> or <code><a href="System-IO.html#v:LineBuffering">LineBuffering</a></code> to <code><a href="System-IO.html#v:NoBuffering">NoBuffering</a></code>, then </p><ul><li> if <code>hdl</code> is writable, the buffer is flushed as for <code><a href="System-IO.html#v:hFlush">hFlush</a></code>; </li><li> if <code>hdl</code> is not writable, the contents of the buffer is discarded. </li></ul><p>This operation may fail with: </p><ul><li> <code>isPermissionError</code> if the handle has already been used for reading or writing and the implementation does not allow the buffering mode to be changed. </li></ul></div></div><div class="top"><p class="src"><a name="v:hGetBuffering" class="def">hGetBuffering</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="System-IO.html#t:BufferMode">BufferMode</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hGetBuffering" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hGetBuffering">hGetBuffering</a></code> <code>hdl</code> returns the current buffering mode for <code>hdl</code>. </p></div></div><div class="top"><p class="src"><a name="v:hFlush" class="def">hFlush</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hFlush" class="link">Source</a></p><div class="doc"><p>The action <code><a href="System-IO.html#v:hFlush">hFlush</a></code> <code>hdl</code> causes any items buffered for output in handle <code>hdl</code> to be sent immediately to the operating system. </p><p>This operation may fail with: </p><ul><li> <code>isFullError</code> if the device is full; </li><li> <code>isPermissionError</code> if a system resource limit would be exceeded. It is unspecified whether the characters in the buffer are discarded or retained under these circumstances. </li></ul></div></div><h2 id="g:13">Repositioning handles </h2><div class="top"><p class="src"><a name="v:hGetPosn" class="def">hGetPosn</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="System-IO.html#t:HandlePosn">HandlePosn</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hGetPosn" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hGetPosn">hGetPosn</a></code> <code>hdl</code> returns the current I/O position of <code>hdl</code> as a value of the abstract type <code><a href="System-IO.html#t:HandlePosn">HandlePosn</a></code>. </p></div></div><div class="top"><p class="src"><a name="v:hSetPosn" class="def">hSetPosn</a> :: <a href="System-IO.html#t:HandlePosn">HandlePosn</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hSetPosn" class="link">Source</a></p><div class="doc"><p>If a call to <code><a href="System-IO.html#v:hGetPosn">hGetPosn</a></code> <code>hdl</code> returns a position <code>p</code>, then computation <code><a href="System-IO.html#v:hSetPosn">hSetPosn</a></code> <code>p</code> sets the position of <code>hdl</code> to the position it held at the time of the call to <code><a href="System-IO.html#v:hGetPosn">hGetPosn</a></code>. </p><p>This operation may fail with: </p><ul><li> <code>isPermissionError</code> if a system resource limit would be exceeded. </li></ul></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:HandlePosn" class="def">HandlePosn</a> <a href="../base-4.5.1.0/src/GHC-IO-Handle.html#HandlePosn" class="link">Source</a></p><div class="subs instances"><p id="control.i:HandlePosn" class="caption collapser" onclick="toggleSection('i:HandlePosn')">Instances</p><div id="section.i:HandlePosn" class="show"><table><tr><td class="src"><a href="Prelude.html#t:Eq">Eq</a> <a href="System-IO.html#t:HandlePosn">HandlePosn</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Show">Show</a> <a href="System-IO.html#t:HandlePosn">HandlePosn</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:hSeek" class="def">hSeek</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:SeekMode">SeekMode</a> -> <a href="Prelude.html#t:Integer">Integer</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hSeek" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hSeek">hSeek</a></code> <code>hdl mode i</code> sets the position of handle <code>hdl</code> depending on <code>mode</code>. The offset <code>i</code> is given in terms of 8-bit bytes. </p><p>If <code>hdl</code> 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 <code><a href="System-IO.html#v:hIsSeekable">hIsSeekable</a></code>), 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. </p><p>This operation may fail with: </p><ul><li> <code>isIllegalOperationError</code> if the Handle is not seekable, or does not support the requested seek mode. </li><li> <code>isPermissionError</code> if a system resource limit would be exceeded. </li></ul></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:SeekMode" class="def">SeekMode</a> <a href="../base-4.5.1.0/src/GHC-IO-Device.html#SeekMode" class="link">Source</a></p><div class="doc"><p>A mode that determines the effect of <code>hSeek</code> <code>hdl mode i</code>. </p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:AbsoluteSeek" class="def">AbsoluteSeek</a></td><td class="doc"><p>the position of <code>hdl</code> is set to <code>i</code>. </p></td></tr><tr><td class="src"><a name="v:RelativeSeek" class="def">RelativeSeek</a></td><td class="doc"><p>the position of <code>hdl</code> is set to offset <code>i</code> from the current position. </p></td></tr><tr><td class="src"><a name="v:SeekFromEnd" class="def">SeekFromEnd</a></td><td class="doc"><p>the position of <code>hdl</code> is set to offset <code>i</code> from the end of the file. </p></td></tr></table></div><div class="subs instances"><p id="control.i:SeekMode" class="caption collapser" onclick="toggleSection('i:SeekMode')">Instances</p><div id="section.i:SeekMode" class="show"><table><tr><td class="src"><a href="Prelude.html#t:Enum">Enum</a> <a href="System-IO.html#t:SeekMode">SeekMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Eq">Eq</a> <a href="System-IO.html#t:SeekMode">SeekMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Ord">Ord</a> <a href="System-IO.html#t:SeekMode">SeekMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Read">Read</a> <a href="System-IO.html#t:SeekMode">SeekMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Show">Show</a> <a href="System-IO.html#t:SeekMode">SeekMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Ix.html#t:Ix">Ix</a> <a href="System-IO.html#t:SeekMode">SeekMode</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:hTell" class="def">hTell</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Integer">Integer</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hTell" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hTell">hTell</a></code> <code>hdl</code> returns the current position of the handle <code>hdl</code>, as the number of bytes from the beginning of the file. The value returned may be subsequently passed to <code><a href="System-IO.html#v:hSeek">hSeek</a></code> to reposition the handle to the current position. </p><p>This operation may fail with: </p><ul><li> <code>isIllegalOperationError</code> if the Handle is not seekable. </li></ul></div></div><h2 id="g:14">Handle properties </h2><div class="doc"><p>Each of these operations returns <code><a href="Prelude.html#v:True">True</a></code> if the handle has the the specified property, or <code><a href="Prelude.html#v:False">False</a></code> otherwise. </p></div><div class="top"><p class="src"><a name="v:hIsOpen" class="def">hIsOpen</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hIsOpen" class="link">Source</a></p></div><div class="top"><p class="src"><a name="v:hIsClosed" class="def">hIsClosed</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hIsClosed" class="link">Source</a></p></div><div class="top"><p class="src"><a name="v:hIsReadable" class="def">hIsReadable</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hIsReadable" class="link">Source</a></p></div><div class="top"><p class="src"><a name="v:hIsWritable" class="def">hIsWritable</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hIsWritable" class="link">Source</a></p></div><div class="top"><p class="src"><a name="v:hIsSeekable" class="def">hIsSeekable</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hIsSeekable" class="link">Source</a></p></div><h2 id="g:15">Terminal operations </h2><div class="top"><p class="src"><a name="v:hIsTerminalDevice" class="def">hIsTerminalDevice</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hIsTerminalDevice" class="link">Source</a></p><div class="doc"><p>Is the handle connected to a terminal? </p></div></div><div class="top"><p class="src"><a name="v:hSetEcho" class="def">hSetEcho</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:Bool">Bool</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hSetEcho" class="link">Source</a></p><div class="doc"><p>Set the echoing status of a handle connected to a terminal. </p></div></div><div class="top"><p class="src"><a name="v:hGetEcho" class="def">hGetEcho</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hGetEcho" class="link">Source</a></p><div class="doc"><p>Get the echoing status of a handle connected to a terminal. </p></div></div><h2 id="g:16">Showing handle state </h2><div class="top"><p class="src"><a name="v:hShow" class="def">hShow</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hShow" class="link">Source</a></p><div class="doc"><p><code><a href="System-IO.html#v:hShow">hShow</a></code> is in the <code><a href="System-IO.html#t:IO">IO</a></code> monad, and gives more comprehensive output than the (pure) instance of <code><a href="Prelude.html#t:Show">Show</a></code> for <code><a href="System-IO.html#t:Handle">Handle</a></code>. </p></div></div><h1 id="g:17">Text input and output </h1><h2 id="g:18">Text input </h2><div class="top"><p class="src"><a name="v:hWaitForInput" class="def">hWaitForInput</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:Int">Int</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hWaitForInput">hWaitForInput</a></code> <code>hdl t</code> waits until input is available on handle <code>hdl</code>. It returns <code><a href="Prelude.html#v:True">True</a></code> as soon as input is available on <code>hdl</code>, or <code><a href="Prelude.html#v:False">False</a></code> if no input is available within <code>t</code> milliseconds. Note that <code><a href="System-IO.html#v:hWaitForInput">hWaitForInput</a></code> waits until one or more full <em>characters</em> are available, which means that it needs to do decoding, and hence may fail with a decoding error. </p><p>If <code>t</code> is less than zero, then <code>hWaitForInput</code> waits indefinitely. </p><p>This operation may fail with: </p><ul><li> <code>isEOFError</code> if the end of file has been reached. </li><li> a decoding error, if the input begins with an invalid byte sequence in this Handle's encoding. </li></ul></div></div><div class="top"><p class="src"><a name="v:hReady" class="def">hReady</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Bool">Bool</a><a href="../base-4.5.1.0/src/System-IO.html#hReady" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hReady">hReady</a></code> <code>hdl</code> indicates whether at least one item is available for input from handle <code>hdl</code>. </p><p>This operation may fail with: </p><ul><li> <code><a href="../base-4.5.1.0/System-IO-Error.html#v:isEOFError">isEOFError</a></code> if the end of file has been reached. </li></ul></div></div><div class="top"><p class="src"><a name="v:hGetChar" class="def">hGetChar</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Char">Char</a><a href="../base-4.5.1.0/src/GHC-IO-Handle-Text.html#hGetChar" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hGetChar">hGetChar</a></code> <code>hdl</code> reads a character from the file or channel managed by <code>hdl</code>, blocking until a character is available. </p><p>This operation may fail with: </p><ul><li> <code><a href="System-IO-Error.html#v:isEOFError">isEOFError</a></code> if the end of file has been reached. </li></ul></div></div><div class="top"><p class="src"><a name="v:hGetLine" class="def">hGetLine</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a><a href="../base-4.5.1.0/src/GHC-IO-Handle-Text.html#hGetLine" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hGetLine">hGetLine</a></code> <code>hdl</code> reads a line from the file or channel managed by <code>hdl</code>. </p><p>This operation may fail with: </p><ul><li> <code><a href="System-IO-Error.html#v:isEOFError">isEOFError</a></code> if the end of file is encountered when reading the <em>first</em> character of the line. </li></ul><p>If <code><a href="System-IO.html#v:hGetLine">hGetLine</a></code> 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. </p></div></div><div class="top"><p class="src"><a name="v:hLookAhead" class="def">hLookAhead</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Char">Char</a><a href="../base-4.5.1.0/src/GHC-IO-Handle.html#hLookAhead" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hLookAhead">hLookAhead</a></code> returns the next character from the handle without removing it from the input buffer, blocking until a character is available. </p><p>This operation may fail with: </p><ul><li> <code>isEOFError</code> if the end of file has been reached. </li></ul></div></div><div class="top"><p class="src"><a name="v:hGetContents" class="def">hGetContents</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a><a href="../base-4.5.1.0/src/GHC-IO-Handle-Text.html#hGetContents" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hGetContents">hGetContents</a></code> <code>hdl</code> returns the list of characters corresponding to the unread portion of the channel or file managed by <code>hdl</code>, which is put into an intermediate state, <em>semi-closed</em>. In this state, <code>hdl</code> is effectively closed, but items are read from <code>hdl</code> on demand and accumulated in a special list returned by <code><a href="System-IO.html#v:hGetContents">hGetContents</a></code> <code>hdl</code>. </p><p>Any operation that fails because a handle is closed, also fails if a handle is semi-closed. The only exception is <code>hClose</code>. A semi-closed handle becomes closed: </p><ul><li> if <code>hClose</code> is applied to it; </li><li> if an I/O error occurs when reading an item from the handle; </li><li> or once the entire contents of the handle has been read. </li></ul><p>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. </p><p>Any I/O errors encountered while a handle is semi-closed are simply discarded. </p><p>This operation may fail with: </p><ul><li> <code><a href="System-IO-Error.html#v:isEOFError">isEOFError</a></code> if the end of file has been reached. </li></ul></div></div><h2 id="g:19">Text output </h2><div class="top"><p class="src"><a name="v:hPutChar" class="def">hPutChar</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:Char">Char</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle-Text.html#hPutChar" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hPutChar">hPutChar</a></code> <code>hdl ch</code> writes the character <code>ch</code> to the file or channel managed by <code>hdl</code>. Characters may be buffered if buffering is enabled for <code>hdl</code>. </p><p>This operation may fail with: </p><ul><li> <code><a href="System-IO-Error.html#v:isFullError">isFullError</a></code> if the device is full; or </li><li> <code><a href="System-IO-Error.html#v:isPermissionError">isPermissionError</a></code> if another system resource limit would be exceeded. </li></ul></div></div><div class="top"><p class="src"><a name="v:hPutStr" class="def">hPutStr</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle-Text.html#hPutStr" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hPutStr">hPutStr</a></code> <code>hdl s</code> writes the string <code>s</code> to the file or channel managed by <code>hdl</code>. </p><p>This operation may fail with: </p><ul><li> <code><a href="System-IO-Error.html#v:isFullError">isFullError</a></code> if the device is full; or </li><li> <code><a href="System-IO-Error.html#v:isPermissionError">isPermissionError</a></code> if another system resource limit would be exceeded. </li></ul></div></div><div class="top"><p class="src"><a name="v:hPutStrLn" class="def">hPutStrLn</a> :: <a href="System-IO.html#t:Handle">Handle</a> -> <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/GHC-IO-Handle-Text.html#hPutStrLn" class="link">Source</a></p><div class="doc"><p>The same as <code><a href="System-IO.html#v:hPutStr">hPutStr</a></code>, but adds a newline character. </p></div></div><div class="top"><p class="src"><a name="v:hPrint" class="def">hPrint</a> :: <a href="Prelude.html#t:Show">Show</a> a => <a href="System-IO.html#t:Handle">Handle</a> -> a -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/System-IO.html#hPrint" class="link">Source</a></p><div class="doc"><p>Computation <code><a href="System-IO.html#v:hPrint">hPrint</a></code> <code>hdl t</code> writes the string representation of <code>t</code> given by the <code><a href="Prelude.html#v:shows">shows</a></code> function to the file or channel managed by <code>hdl</code> and appends a newline. </p><p>This operation may fail with: </p><ul><li> <code><a href="../base-4.5.1.0/System-IO-Error.html#v:isFullError">isFullError</a></code> if the device is full; or </li><li> <code><a href="../base-4.5.1.0/System-IO-Error.html#v:isPermissionError">isPermissionError</a></code> if another system resource limit would be exceeded. </li></ul></div></div><h2 id="g:20">Special cases for standard input and output </h2><div class="doc"><p>These functions are also exported by the <a href="../base-4.5.1.0/Prelude.html">Prelude</a>. </p></div><div class="top"><p class="src"><a name="v:interact" class="def">interact</a> :: (<a href="Prelude.html#t:String">String</a> -> <a href="Prelude.html#t:String">String</a>) -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/System-IO.html#interact" class="link">Source</a></p><div class="doc"><p>The <code><a href="System-IO.html#v:interact">interact</a></code> function takes a function of type <code>String->String</code> 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. </p></div></div><div class="top"><p class="src"><a name="v:putChar" class="def">putChar</a> :: <a href="Prelude.html#t:Char">Char</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/System-IO.html#putChar" class="link">Source</a></p><div class="doc"><p>Write a character to the standard output device (same as <code><a href="System-IO.html#v:hPutChar">hPutChar</a></code> <code><a href="System-IO.html#v:stdout">stdout</a></code>). </p></div></div><div class="top"><p class="src"><a name="v:putStr" class="def">putStr</a> :: <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/System-IO.html#putStr" class="link">Source</a></p><div class="doc"><p>Write a string to the standard output device (same as <code><a href="System-IO.html#v:hPutStr">hPutStr</a></code> <code><a href="System-IO.html#v:stdout">stdout</a></code>). </p></div></div><div class="top"><p class="src"><a name="v:putStrLn" class="def">putStrLn</a> :: <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/System-IO.html#putStrLn" class="link">Source</a></p><div class="doc"><p>The same as <code><a href="System-IO.html#v:putStr">putStr</a></code>, but adds a newline character. </p></div></div><div class="top"><p class="src"><a name="v:print" class="def">print</a> :: <a href="Prelude.html#t:Show">Show</a> a => a -> <a href="System-IO.html#t:IO">IO</a> ()<a href="../base-4.5.1.0/src/System-IO.html#print" class="link">Source</a></p><div class="doc"><p>The <code><a href="System-IO.html#v:print">print</a></code> function outputs a value of any printable type to the standard output device. Printable types are those that are instances of class <code><a href="Prelude.html#t:Show">Show</a></code>; <code><a href="System-IO.html#v:print">print</a></code> converts values to strings for output using the <code><a href="Prelude.html#v:show">show</a></code> operation and adds a newline. </p><p>For example, a program to print the first 20 integers and their powers of 2 could be written as: </p><pre> main = print ([(n, 2^n) | n <- [0..19]]) </pre></div></div><div class="top"><p class="src"><a name="v:getChar" class="def">getChar</a> :: <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:Char">Char</a><a href="../base-4.5.1.0/src/System-IO.html#getChar" class="link">Source</a></p><div class="doc"><p>Read a character from the standard input device (same as <code><a href="System-IO.html#v:hGetChar">hGetChar</a></code> <code><a href="System-IO.html#v:stdin">stdin</a></code>). </p></div></div><div class="top"><p class="src"><a name="v:getLine" class="def">getLine</a> :: <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a><a href="../base-4.5.1.0/src/System-IO.html#getLine" class="link">Source</a></p><div class="doc"><p>Read a line from the standard input device (same as <code><a href="System-IO.html#v:hGetLine">hGetLine</a></code> <code><a href="System-IO.html#v:stdin">stdin</a></code>). </p></div></div><div class="top"><p class="src"><a name="v:getContents" class="def">getContents</a> :: <a href="System-IO.html#t:IO">IO</a> <a href="Prelude.html#t:String">String</a><a href="../base-4.5.1.0/src/System-IO.html#getContents" class="link">Source</a></p><div class="doc"><p>The <code><a href="System-IO.html#v:getContents">getContents</a></code> operation returns all user input as a single string, which is read lazily as it is needed (same as <code><a href="System-IO.html#v:hGetContents">hGetContents</a></code> <code><a href="System-IO.html#v:stdin">stdin</a></code>). </p></div></div><div class="top"><p class="src"><a name="v:readIO" class="def">readIO</a> :: <a href="Prelude.html#t:Read">Read</a> a => <a href="Prelude.html#t:String">String</a> -> <a href="System-IO.html#t:IO">IO</a> a<a href="../base-4.5.1.0/src/System-IO.html#readIO" class="link">Source</a></p><div class="doc"><p>The <code><a href="System-IO.html#v:readIO">readIO</a></code> function is similar to <code><a href="Prelude.html#v:read">read</a></code> except that it signals parse failure to the <code><a href="System-IO.html#t:IO">IO</a></code> monad instead of terminating the program. </p></div></div><div class="top"><p class="src"><a name="v:readLn" class="def">readLn</a> :: <a href="Prelude.html#t:Read">Read</a> a => <a href="System-IO.html#t:IO">IO</a> a<a href="../base-4.5.1.0/src/System-IO.html#readLn" class="link">Source</a></p><div class="doc"><p>The <code><a href="System-IO.html#v:readLn">readLn</a></code> function combines <code><a href="System-IO.html#v:getLine">getLine</a></code> and <code><a href="System-IO.html#v:readIO">readIO</a></code>. </p></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.11.0</p></div></body></html>