"Appending" at beginning of file

Discussion in 'C++' started by Bruno, Jun 2, 2006.

  1. Bruno

    Bruno Guest

    Hi all!

    I'm new to this group, and I have a question: is there any way to open
    a file and write something _before_ it's contents? Example:

    original file:

    ABCDE

    appended file:

    FG <---------------- put before the original content.
    ABCDE

    At present, i have to create a new file, write the new content, then
    copy the contents of the original file to the new file. This is such a
    pain in the neck!

    Best regards,

    Bruno.
    Bruno, Jun 2, 2006
    #1
    1. Advertising

  2. Bruno wrote:
    > I'm new to this group, and I have a question: is there any way to open
    > a file and write something _before_ it's contents?


    No.

    > [..]
    >
    > At present, i have to create a new file, write the new content, then
    > copy the contents of the original file to the new file. This is such a
    > pain in the neck!


    Perhaps your neck needs to review its pain threshold.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 2, 2006
    #2
    1. Advertising

  3. Bruno

    Tomás Guest

    Bruno posted:


    > At present, i have to create a new file, write the new content, then
    > copy the contents of the original file to the new file. This is such a
    > pain in the neck!



    That's how it's done portably as far as I know.


    -Tomás
    Tomás, Jun 2, 2006
    #3
  4. Bruno

    mlimber Guest

    Bruno wrote:
    > is there any way to open
    > a file and write something _before_ it's contents?


    If you're doing that a lot, consider other alternatives so you can use
    standard file access operations:

    1. Reverse the order of the data in your file so that inserting data at
    the beginning means writing to the end.

    2. Add to the file some indicator of a data item's place in the list,
    and then sort the data appropriately when you read it in (occasionally
    you might also re-write the whole file in the sorted order to ease load
    times).

    Etc.

    Cheers! --M
    mlimber, Jun 2, 2006
    #4
    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. scopp
    Replies:
    3
    Views:
    423
    Guest
    Jan 22, 2004
  2. MBALOVER
    Replies:
    9
    Views:
    2,241
    Richard Bos
    Mar 17, 2010
  3. Jesse B.
    Replies:
    9
    Views:
    217
    Jesse B.
    Mar 27, 2010
  4. Replies:
    3
    Views:
    144
    Klaus Eichner
    Jul 20, 2005
  5. PerlFAQ Server
    Replies:
    0
    Views:
    370
    PerlFAQ Server
    Feb 24, 2011
Loading...

Share This Page