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

Discussion in 'C++' started by sm, Apr 29, 2005.

  1. sm

    sm Guest

    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
     
    sm, Apr 29, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Apr 29, 2005
    #2
    1. Advertising

  3. sm

    Rolf Magnus Guest

    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
     
    Rolf Magnus, Apr 29, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nico Grubert
    Replies:
    2
    Views:
    1,295
    Max Erickson
    Sep 22, 2005
  2. HYRY
    Replies:
    10
    Views:
    608
    Bruno Desthuilliers
    Sep 26, 2007
  3. write to existing file append

    , Aug 22, 2005, in forum: ASP General
    Replies:
    2
    Views:
    204
  4. Googy
    Replies:
    6
    Views:
    150
    Keith Keller
    Jan 15, 2007
  5. PerlFAQ Server
    Replies:
    0
    Views:
    390
    PerlFAQ Server
    Feb 24, 2011
Loading...

Share This Page