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

L. J. djsenda at gmail.com
Sun Aug 27 04:36:32 EDT 2006


On 8/27/06, Donald Bruce Stewart <dons at cse.unsw.edu.au> 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 at haskell.org

 Ok, for another question I will write in this mailing list.


More information about the Haskell mailing list