[Haskell-cafe] Efficient way to edit a file

David Roundy droundy at darcs.net
Fri Jun 2 06:10:54 EDT 2006

On Fri, Jun 02, 2006 at 12:34:51PM +1000, Donald Bruce Stewart wrote:
> dons:
> > briqueabraque:
> > >   Hi,
> > > 
> > >   I need to edit big text files (5 to 500 Mb). But I just need to 
> > > change one or two small lines, and save it. What is the best way to do 
> > > that in Haskell, without creating copies of the whole files?
> > > 
> Thinking further, since you want to avoid copying on the disk, you need
> to be able to keep the edited version in memory. So the strict
> bytestring would be best, for example:

dons is right here, but I'd add that it's hard to safely edit a big
file without creating a copy, if you want your program to leave the
file in a consistent state even if it crashes (power failure, kill,
file server failure).  dons' suggestion could leave you with a deleted
file (if power goes down at the beginning of a write).  If you aren't
changing the size of the file, opening it ReadWrite will allow you to
modify it reasonably safeily.  If you *are* changing its size, then
doing something explicit would probably be the way to go (and I'd
probably actually use mmap and memmove to make the change, if you do
need to modify the file size).  But then, I'm thinking posix (as I
generally do), which may not be the case for you.  And perhaps you
don't need to be careful.  I've found that if bad things can happen,
they do.  But that's largely because darcs has lots of users...
David Roundy

More information about the Haskell-Cafe mailing list