f



"Insert" (not append) new text segment to an existing text file

How to "Insert" (not append) new text segment to an existing text file?

Assume that we have text file as shown below;
     Elvis
     Sofia
     Kylix
BCB--> How to insert here?
     Atten
     BuilderX
     Roma
     Together
     Cairo
0
someone15 (472)
4/29/2005 1:18:52 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

2 Replies
695 Views

Similar Articles

[PageSpeed] 22

sm wrote:
> How to "Insert" (not append) new text segment to an existing text file?
> 
> Assume that we have text file as shown below;
>     Elvis
>     Sofia
>     Kylix
> BCB--> How to insert here?
>     Atten
>     BuilderX
>     Roma
>     Together
>     Cairo

This has nothing to do with the language.  Open the file for reading,
open another file (new one) for writing.  Begin reading the first one
and writing to the new one until you hit the spot where you need to
insert something.  Now, write what you need inserted.  Once done, go
on with "copying" the contents of the original file.  Another possible
solution is to read the entire contents of the file into some memory
structure, insert what you need into that structure, then write that
structure out to the same file again (after re-opening it for writing).

V
0
v.Abazarov (13256)
4/29/2005 3:45:58 PM
sm wrote:

> How to "Insert" (not append) new text segment to an existing text file?

You cannot insert new data into the file. You can replace existing data with
new data (as long as the size stays the same), and you can append, but you
cannot add or remove data at any other place than the end.
If you need that, you basically have to copy the whole contents of the file
over to a new file, inserting the new data at the right place. Then rename
it back to the original file name.

> Assume that we have text file as shown below;
>      Elvis
>      Sofia
>      Kylix
> BCB--> How to insert here?
>      Atten
>      BuilderX
>      Roma
>      Together
>      Cairo

0
ramagnus (3487)
4/29/2005 3:55:35 PM
Reply: