[Haskell-cafe] Silly I/O question
jgoerzen at complete.org
Tue Sep 28 16:05:41 EDT 2004
I'm trying to write a program that will copy an arbitrarily large text
file to a destination, and duplicate it 100 times.
./myprog < Input > Output
would be the same as running:
cat Input > Output # once
cat Input >> Output # 99 times
My first attempt was this:
main = disp 100
disp 0 = return ()
disp n = do
c <- getContents
hSeek stdin AbsoluteSeek 0
That failed, though, because getContents closes the file after it's been
completely read (ugh -- why?).
So then I tried to work on various options around hGetLine and
hPutStrLn. But I couldn't figure out a way to make this either properly
tail-recursive while handling the exception, or to avoid polling for
EOF each time through the function.
I also don't want to store the entire file in memory -- the idea is to
seek back to the beginning for each iteration. I'm assuming that stdin
is a seekable fd.
I checked the wiki for a pattern here but didn't see any.
More information about the Haskell-Cafe