[Haskell-cafe] Newbie question
droundy at darcs.net
Fri Aug 12 08:58:07 EDT 2005
On Fri, Aug 12, 2005 at 02:54:03PM +0200, Lemmih wrote:
> On 8/12/05, David Roundy <droundy at darcs.net> wrote:
> > On Fri, Aug 12, 2005 at 09:17:32AM -0300, Andr Vargas Abs da Cruz wrote:
> > > readDataFromFile filename = do
> > > bracket (openFile filename ReadMode) hClose
> > > (\h -> do contents <- hGetContents h
> > > return (lines contents))
> > >
> > > The question is: if i try to run this, it returns me nothing (just
> > > an empty list). Why does this happen ? When i remove the "return" and
> > > put a "print" instead, it prints everything as i expected.
> > I think that this may be because hGetContents puts the file in a
> > semi-closed state (and reads the contents lazily). Try removing the
> > hClose (replacing it with (return ()). Lazy IO is nice, but gets
> > tricky at times.
> Or better yet, use 'readFile'.
Indeed, that would be simpler, but it's still lazy IO, and if you're
unaware of that you'll still get confused when you try to delete the file
after reading the lines you wanted out of it, and it fails on windows, but
not on POSIX systems... unless you read the entire file, or computed its
More information about the Haskell-Cafe