View Single Post
Old Aug 26th, 2006, 1:25 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Yes and no. You can alter individual bytes of a file. So in the example you provide, where "four" is the same length as the string to replace it, "vier", one could just alter those four bytes. If the size of the word differs, however, one would have to rewrite all bytes in the file that come after it.

For example, if I wished to replace "out" with "in":
hello out there
hello in there
I'd have to move "there" back one character, as the file size has decreased by one. This would involve rewriting the latter half of the file (highlighted in red).

Thus, for most search and replace operations, there's no real disadvantage to reading the entire file into RAM and altering it there (unless you're likely to run out of RAM).
Arevos is offline   Reply With Quote