Thanks Antoine!<br><br>I was suspecting it should be the lazyness of hGetContents yesterday, before going to sleep. <br><br>You just confirmed that for me!<br><br><div class="gmail_quote">On Tue, Jan 20, 2009 at 01:44, Antoine Latter <span dir="ltr">&lt;<a href="mailto:aslatter@gmail.com">aslatter@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">On Mon, Jan 19, 2009 at 7:30 PM, Rafael Gustavo da Cunha Pereira Pinto<br>

&lt;<a href="mailto:RafaelGCPP.Linux@gmail.com">RafaelGCPP.Linux@gmail.com</a>&gt; wrote:<br>
&gt; Could someone explain why:<br>
&gt;<br>
&gt; main= do<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; h&lt;-openFile &quot;test.cir&quot; ReadMode<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c&lt;-hGetContents h<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print c<br>
&gt;<br>
&gt;&gt; runhaskell test1.hs<br>
&gt;&gt; &quot;* Teste\n\nR1 &nbsp;1 0 10\nC1 &nbsp;1 0 10uF\nI1 &nbsp;1 0 1mA\n\n.DC \n.PRINT\n&quot;<br>
&gt;<br>
&gt; works and<br>
&gt;<br>
&gt;<br>
&gt; main= (withFile &quot;test.cir&quot; ReadMode hGetContents) &gt;&gt;= print<br>
&gt;<br>
&gt;&gt; runhaskell test1.hs<br>
&gt;&gt; &quot;&quot;<br>
&gt;<br>
&gt; don&#39;t?<br>
<br>
</div>&#39;hGetContents&#39; is a lazy-IO function, which means doesn&#39;t really start<br>
reading from the handle until another function tries to consume its<br>
output.<br>
<br>
The problem is that &#39;print&#39; - the consumer - is outside of the<br>
&#39;withFile&#39; argument, and &#39;withFile&#39; guarantees that the file is closed<br>
when it finishes execution.<br>
<br>
So by the time &#39;hGetContents&#39; tries to do its thing, the file handle is closed.<br>
<br>
This snippet:<br>
<br>
&gt; main = withFile &quot;test.cir&quot; ReadMode $ \h -&gt; hGetContents h &gt;&gt;= print<br>
<br>
puts the call to &#39;print&#39; inside the argument to &#39;withFile&#39;, so it<br>
should work as expected.<br>
<font color="#888888"><br>
-Antoine<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Rafael Gustavo da Cunha Pereira Pinto<br>Electronic Engineer, MSc.<br>