Howard said:
The standard method is to read the file in, writing out to a new file all
the text up to the "insertion point", then writing out the text you're
inserting, then writing out the rest of the file. (But you're writing to a
different file, you understand. You can always rename the file(s), of
course, so that it looks like you're writing to the "same" file.)
One possible approach for single file implementation would be:
- copy string to circular buffer
- open and io stream for read/write
- find insertion point in io stream
- loop while !eof
- read char from stream
- push back char to buffer
- (over)write char to stream from front of circular buffer
- pop front
- loop while !buffer.empty()
- write char to stream from front of circular buffer
- pop front
Any comments from those more familiar with streams? Performance relative to
other methods would certainly need to be measured. I could see using this
approach when inserting relatively small string near the tail of a very
large file.
Jeff F