[Haskell-cafe] Automatic file closing after readFile

Don Stewart dons at galois.com
Thu Oct 18 12:34:21 EDT 2007


magnus:
> On Thu, Oct 18, 2007 at 12:05:40 +0100, Jules Bean wrote:
> >>Is there some (easy) way to avoid this while still using readFile?
> >
> >readFile' f = do s <- readFile f
> >		 return (length s `seq` s)
> >
> >(and curse the fact that the default readFile is unsafelazy).
> 
> :( Doesn't work.  I'm starting to suspect there is no automatic close of
> opened files even after they've been completely read.
> 

I'd also suggest using strict io here, either vi:

    readFile' f = do s <- readFile f
                     length s `seq` return s -- n.b Jules :)

or

    Data.ByteString.readFile 

-- Don


More information about the Haskell-Cafe mailing list