On 8/27/06, Donald Bruce Stewart <[EMAIL PROTECTED]> wrote:
djsenda:
> Hi, I use the operation 'readFile' for obtain information locates on
> a file. When I try to write another information on the same file, I
> obtain this error message: "openFile: permision denied". I found this:
> "The readFile operation holds a semi-closed handle on the file until
> the entire contents of the file have been consumed. It follows that an
> attempt to write to a file (using writeFile, for example) that was
> earlier opened by readFile will usually result in failure with
> isAlreadyInUseError." in this web
> http://www.haskell.org/onlinereport/io.html.
>
> How can I break that semi-closed handle for to write in the
> preaviously readed file? Thank you.
Due to lazy IO, you'll need to either read the entire file first, before
you can safely write to that file, (or explicitly close the Handle):
import Control.Exception
main = do
s <- readFile "x"
evaluate $ length s -- evaluate the list, reading it in
writeFil e "x" (reverse s)
GHCi says me that: evaluate is not in scope
Here we use 'evaluate (length s)' to explictily force the entire file to
be read, which will close the Handle, and allows us to safely write back
to the same file.
-- Don
P.S. Learning Haskell questions are better addressed to haskell-cafe@haskell.org
Ok, for another question I will write in this mailing list.
_______________________________________________
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell