A problem about hGetContents

Glynn Clements glynn.clements@virgin.net
Sun, 19 Jan 2003 17:19:52 +0000


Nick Name wrote:

> I would like to use hGetContents just to retrieve the list of the lin=
es
> of a file, but if I code a function like:
>=20
> linesFromFile :: FilePath -> IO [String]
> linesFromFile f =3D do
> =09h <- openFile f ReadMode
> =09l <- hGetContents h
> =09hClose h
> =09return (lines l)
>=20
> I obviously always get the empty list as a result. How should I code =
the
> function?

Just omit the hClose; hGetContents will automatically close the handle
once all of the data has actually been read. See =A711.2.1 of the
library report for details.

--=20
Glynn Clements <glynn.clements@virgin.net>