reading text files

Peter Simons simons at cryp.to
Mon Oct 4 14:34:42 EDT 2004


Carsten Schultz writes:

 > I want to read a text file from a socket and process it as a String
 > (using Parsec).  Is hGetContent ok, or are there mor efficient
 > alternatives available, even if I want a String anyway?

hGetContents is fine, although you have to be aware that the
function does not implement any timeouts. Meaning, if you
don't receive the data you need, your program will hang.

Whether your application is fast or not doesn't really
depend on how you read the data, but on how you _process_
it. Personally, I've always tried to stay away from
hGetContents and friends, because once you have the "whole"
list at your disposal, the temptation is huge to write code
that treats the data as if you _had_ it all in memory, and
that usually is very inefficient. I'd rather read the input
in blocks, or lines, and process it as such.

Peter



More information about the Glasgow-haskell-users mailing list