Commit 6e67363d authored by simonmar's avatar simonmar
Browse files

[project @ 2004-02-25 10:34:44 by simonmar]

Add an entry to the FAQ about buffering of stdout
parent 29fd4389
......@@ -340,6 +340,38 @@ details.</para>
lose Ctrl-D. C'est la vie.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>If I print out a string using <literal>putStr</literal>,
and then attempt to read some input using
<literal>hGetLine</literal>, I don't see the output from the
<literal>putStr</literal>.</term>
<listitem>
<para>The <literal>stdout</literal> handle is line-buffered by
default, which means that output sent to the handle is only
flushed when a newline (<literal>/n</literal>) is output, the
buffer is full, or <literal>hFlush</literal> is called on the
Handle. The right way to make the text appear without sending
a newline is to use <literal>hFlush</literal>:</para>
<programlisting>
import System.IO
main = do
putStr "how are you today? "
hFlush stdout
input <- hGetLine
...</programlisting>
<para>You'll probably find that the behaviour differs when
using GHCi: the <literal>hFlush</literal> isn't necessary to
make the text appear. This is because in GHCi we turn off the
buffering on <literal>stdout</literal>, because this is
normally what you want in an interpreter: output appears as it
is generated.</para>
</listitem>
</varlistentry>
</variablelist>
</chapter>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment