[Haskell-beginners] File I/O: reading from, writing to, same file in one function

Geoffrey Bays charioteer7 at gmail.com
Thu Feb 5 21:43:29 UTC 2015


Chaddai:
Thanks for your excellent explanation of why using a temp file is necessary
and a good thing to do.

The problem also with using withFile and a lambda is that in my infinite
Haskell beginnerness, I do not know how to get the
contents out of the lambda for use later for the second try of withFile in
WriteMode.

As in LYAH

   1.     withFile "girlfriend.txt" ReadMode (\handle -> do
   2.         contents <- hGetContents handle
   3.         putStr contents)

how to retrieve contents for later use? Scope of contents variable in
inside lambda only it would appear.

Can I declare an empty contents IO String before the withFile lambda
somehow?


Thanks


On Thu, Feb 5, 2015 at 4:33 PM, Chaddaï Fouché <chaddai.fouche at gmail.com>
wrote:

> On Thu, Feb 5, 2015 at 10:30 PM, Geoffrey Bays <charioteer7 at gmail.com>
> wrote:
>
>> Francesco:
>>
>> Unfortunately, using withFile I get the same error as before with
>> handles: hPutStr: illegal operation (handle is closed).
>>
>>
> This error suggests you're forgetting to reopen your file in WriteMode
> once withFile closed it (you *did *read the documentation of withFile ?).
> Trying to write on a closed handle is an exercise in futility.
>
> --
> Jedaï
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150205/82740db0/attachment.html>


More information about the Beginners mailing list