Flzw wrote:
you open the input file
you create and open an intermediate file
There's no way to do it with only one file open for both reading and wrting
? because I will have to do several (maybe many) replacements one by one, so
copying the whole file x times, deleting then renaming looks like a lot of
overhead...
You can overwrite the text, if and only if, the size of the old text is
the same as the new text.
The C++ language has no facilities for inserting text into the middle
of a file. This has to do with the nature of sequential files. Imagine
a long tape with files one after another with no room between them.
In order to expand one file, all the other ones have to be moved further
down the tape. Big process. So, a new file is created at the end of
the tape containing the new data. The old file is marked as deleted.
If you have a lot of insertions, you may want to create a linked list
of the corrections. The original data is written to the new file until
the first item in the list. Then the list item is written to the new
file. More data from the original file is copied over until the next
item in the list is reached, then it is written to the file. Repeat
until the original file hits EOF.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book