[Haskell-cafe] readFile and closing a file

David Roundy droundy at darcs.net
Thu Sep 18 14:42:05 EDT 2008


On Thu, Sep 18, 2008 at 05:38:51PM +0200, Henning Thielemann wrote:
> On Wed, 17 Sep 2008, Mitchell, Neil wrote:
>> I tend to use openFile, hGetContents, hClose - your initial readFile
>> like call should be openFile/hGetContents, which gives you a lazy
>> stream, and on a parse error call hClose.
>
> Yes, this seems to be the right way. This weakness should be mentioned in
> the docs of readFile. In production code, you cannot rely on a garbage
> collector to close the file, because you might want to open a file for
> writing after you have processed it with readFile, and there is warranty
> that the file is already closed then.

Ah, so you must be concerned about windows systems? Yes, on windows
systems it's definitely best to never to lazy IO.  On non-windows file
systems, unless you're planning on opening thousands of files, relying
on the garbage collector is fine.

David


More information about the Haskell-Cafe mailing list