CrLfFileStream oldFileNamed:

Lex Spoon lex at cc.gatech.edu
Sat Nov 8 15:41:08 UTC 2003


"Germán S. Arduino" <gsa at softhome.net> wrote:
> Says, "The prior contents may be modified or replaced". I need to modify
> some lines and let unmodified anothers (in each .desktop file) but I can't
> figure out how to make that.
> 

It depends on exactly what you want to do, but in general it is tricky
to update a text file in place.  If you make a line shorter, for
example, then you need to shift everything after that line backwards. 
If you make it longer, you need to shift everything forwards.  About the
only two modifications that are easy to make in place, are:

	1. Replacing text by text of an equal length.  (Though, beware of line
ending issues, if either the replacement or the replacee has line
endings!)

	2. Appending to the end of the file.

Unix utilities tend to avoid in-place modification, and instead read in
a copy of the file, write it to a new file, and make changes along the
way.  When everything is done, the original file is deleted and the new
file moved on top of it.  Maybe this will work better for you.  While it
sounds inefficient, it works out fine in practice whenever the text file
is not humongous.  Besides, if you really want efficiency, you wouldn't
be using a text file at all, but would be using a database.  :)


-Lex



More information about the Squeak-dev mailing list