[Haskell-cafe] Strange behaviour with writeFile

Neil Davies semanticphilosopher at googlemail.com
Sun Feb 4 11:11:19 EST 2007

Its about the lazyness of reading the file. The handles on the file
associated (underlying readFile) is still open - hence the resource
being in use.

When you add that extra line the act of writing out the remainer
causes the rest of the input to be fully evaluated and hence the
filehandle is closed.

If you wish to overwrite the existing file you have to assure that the
file is not open for reading - just like with any file interface.


On 04/02/07, C.M.Brown <cmb21 at kent.ac.uk> wrote:
> Hi,
> I am observing some rather strange behaviour with writeFile.
> Say I have the following code:
> answer <- AbstractIO.readFile "filename"
> let (answer2, remainder) = parseAnswer answer
> if remainder == "" && answer2 == ""
>   then do
>     AbstractIO.putStrLn $ "completed"
>   else do
>     AbstractIO.putStrLn answer2
>     AbstractIO.writeFile "filename" remainder
> With the above I get an error saying the resources to "filename" are
> locked. If I add the line "AbstractIO.putStrLn $ show (answer2, remainder)
> before I call writeFile it suddenly magically works!
> Has anyone seen strange behaviour like this before?
> Regards,
> Chris.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list