[Haskell] [Haskell - I/O] Problem with 'readFile'

Udo Stenzel u.stenzel at web.de
Sat Aug 26 18:51:01 EDT 2006

L. J. wrote:
> Hi, I use the operation 'readFile' [...]
> How can I break that semi-closed handle for to write in the
> preaviously readed file? Thank you.

Not at all.  But you can get the same effect you get from 'readFile' if
you use 'openFile' and 'hGetContents'.  If you do the latter, you can
really close the semi-closed handle by calling 'hClose'.

However, chances are, that you will not have consumed the whole file at
the point where you close it (lazy evaluation can^W will be tricky), you
will find that you write a new file with empty content, because
apparently you read an empty file when in fact it wasn't empty, and so
on.  In short: don't do that.  Also, don't fight the error message,
understand, that it saved you from trashing a perfectly good file.

Instead, 'readFile' your data, 'writeFile' it into a new(!) file, then
'renameFile' the new over the old one.  Before doing anything else, get
a deep understanding of lazy IO.  (And this understanding will basically
be, that a database package (gdbm, Berkeley DB, you name it) will serve
you better.)

"The greatest dangers to liberty lurk in insidious encroachment by men
of zeal, well-meaning but without understanding."
	-- Brandeis
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell/attachments/20060827/97cab6a2/attachment.bin

More information about the Haskell mailing list