[Haskell-cafe] Silly I/O question

John Goerzen 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.

Thus:

./myprog < Input > Output

would be the same as running:

cat Input > Output    # once
cat Input >> Output   # 99 times

My first attempt was this:

import IO

main = disp 100

disp 0 = return ()
disp n = do
         c <- getContents
         putStr c
         hSeek stdin AbsoluteSeek 0
         disp (n-1)

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.

Suggestions?





More information about the Haskell-Cafe mailing list