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

S

sm

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
 
V

Victor Bazarov

sm said:
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
 
R

Rolf Magnus

sm said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top