Re: Modifying a file w/o creating a temp file



Bernie Ohls wrote:
Eric Sosman wrote:
My advice is to stick with the temporary file: The
code will be far simpler, you won't need to worry about
length-changing character encodings, and the program is
likely to run faster (except in the equal-length case or
the contract-with-padding case), because it won't have to
do all that jumping around and sliding back and forth.

Of course there's one simple and obvious solution which has not yet
been mentioned or rejected: read the whole file into memory, modify
it there, truncate the original file, and write the modified data
into it.
This has substantial subtle benefits, balanced against the one flaw
of
being memory hungry and limited by swap space. Benefits include (a)
ease of use, (b) if the original file had hard links to it, they are
not broken, (c) if it had special modes/attributes/ACLs, they are
not
lost, and (d) it never needs more space than the file system has,
whereas a temp-file solution typically needs an amount of unused
disk
space equal to the size of the temp file (unless the temp file is
written to a different file system which raises its own issues).

So this might seem an inelegant technique but it has much to
recommend
it - if you know the files won't be too big, for some definition of
"too big".

Of course, if your program crashes or is killed in the middle, you've
lost the file entirely. One big advantage of the temp file method is
that the rename is atomic, so whatever happens the result is either
the original version of the file or the rewritten one.


.



Relevant Pages

  • Re: Modifying a file w/o creating a temp file
    ... Of course there's one simple and obvious solution which has not yet been mentioned or rejected: read the whole file into memory, modify it there, truncate the original file, and write the modified data into it. ... This has substantial subtle benefits, balanced against the one flaw of being memory hungry and limited by swap space. ... Benefits include ease of use, if the original file had hard links to it, they are not broken, if it had special modes/attributes/ACLs, they are not lost, and it never needs more space than the file system has, whereas a temp-file solution typically needs an amount of unused disk space equal to the size of the temp file. ...
    (comp.lang.java.programmer)
  • Re: fopen: pre-pending text
    ... >> 2) open a temp file for writing ... >> 4) write original file to end of temp file ... >right after verifiying. ... >> 8) open original file for writing at start of file ...
    (comp.os.linux.development.system)
  • Re: vb application locks file
    ... Make a temp file, ... A copied file turns up in the file system with a zero or non-zero ... 3- writer renames to real file ... 4- reader suddenly sees pristine file in readable state ...
    (microsoft.public.vb.general.discussion)
  • Re: File manipulation
    ... >> I'm getting confused on the login. ... one easy way to do that is to use a temp file: ... > * open original file ... > * move the temp file over the original file ...
    (perl.beginners)
  • Re: What does this mean "~$" in front of a file?
    ... >replacing the first two letters in the file with ~$. ... >original file, but it creates and saves this additional file. ... >delete the file to the trash can, ... If you try to delete a temp file while the document is open, ...
    (microsoft.public.word.application.errors)