
Hi all, Suppose my program has one or more persistent files that it reads at or near the beginning of its execution and writes at or near the end. Thus, an extremely simplified model of my program could be
main = do h <- openFile "some-file" ReadMode c <- hGetContents h w <- openFile "some-file" WriteMode hPutStr w (f c)
where f is some arbitrary function. My question is how to do this. It seems like the data from h won't necessarily be forced until f is called when it is written, and there is no guarantee that all of the data will even be used by f until it is written. Thus, I will get a file-locking error when trying to write the file. Do I have to rig things so that I know f will consume all of its input? Is it better to use strict I/O? Is there a better idiom for this entirely? Thanks for all of your help. Alex

Alexander Dunlap wrote:
Hi all,
Suppose my program has one or more persistent files that it reads at or near the beginning of its execution and writes at or near the end. Thus, an extremely simplified model of my program could be
main = do h <- openFile "some-file" ReadMode c <- hGetContents h w <- openFile "some-file" WriteMode hPutStr w (f c)
where f is some arbitrary function.
My question is how to do this. It seems like the data from h won't necessarily be forced until f is called when it is written, and there is no guarantee that all of the data will even be used by f until it is written. Thus, I will get a file-locking error when trying to write the file. Do I have to rig things so that I know f will consume all of its input? Is it better to use strict I/O? Is there a better idiom for this entirely?
I think a fairly common way to solve this is to change your program to basically do main = do h <- openFile "some-file" ReadMode c <- hGetContents h w <- openFile "temp-file" hPutStr w (f c) hClose w renameFile "temp-file" "some-file" Of course there may still be some laziness issues to keep in mind. But I believe that should take care of any locking issues your OS might throw at you. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus Haskell is an even 'redder' pill than Lisp or Scheme. -- PaulPotts
participants (2)
-
Alexander Dunlap
-
Magnus Therning