c++ fstream writing to existing file

Discussion in 'C++' started by Mahesha, Jul 24, 2004.

  1. Mahesha

    Mahesha Guest

    Hello,
    I'm new to C++ and I have requirement to open a existing text file in
    write mode and write 2 new lines of text in the beginning of the file.
    I'm working with fstream standard library.
    If someone could direct me to a sample code to do this that would be
    really helpful.

    Thanks,
    Mahesha
     
    Mahesha, Jul 24, 2004
    #1
    1. Advertising

  2. On 23 Jul 2004 18:01:04 -0700, Mahesha <> wrote:

    > Hello,
    > I'm new to C++ and I have requirement to open a existing text file in
    > write mode and write 2 new lines of text in the beginning of the file.
    > I'm working with fstream standard library.
    > If someone could direct me to a sample code to do this that would be
    > really helpful.
    >
    > Thanks,
    > Mahesha


    That cannot be done in any file system that I know of. With files you can
    write to the end of a file, or you can write over the existing contents of
    a file, but you cannot insert data into the beginning or middle of a file.

    I would try an redesign your program so that you don't have to do this.
    You are trying to use files in a way that they were not designed to be
    used.

    If you really cannot avoid this, then the only way to do what you want is
    to write your two lines to a new file, and then copy the rest of the old
    file to the end of the new file. This is obviously very inefficient and is
    why you should try and avoid if at all possible.

    John
     
    John Harrison, Jul 24, 2004
    #2
    1. Advertising

  3. Mahesha

    Jack Klein Guest

    On 23 Jul 2004 18:01:04 -0700, (Mahesha) wrote in
    comp.lang.c++:

    > Hello,
    > I'm new to C++ and I have requirement to open a existing text file in
    > write mode and write 2 new lines of text in the beginning of the file.
    > I'm working with fstream standard library.
    > If someone could direct me to a sample code to do this that would be
    > really helpful.
    >
    > Thanks,
    > Mahesha


    This can't be done this way in standard C++, and probably can't be
    done this way even with non-standard platform specific extensions on
    most platforms.

    There is no function to "push down" the existing contents of a file to
    make room for some arbitrary number of new characters.

    What you need to do is create a file with some temporary name. Write
    your two new lines to it, then copy the data from the original file
    into it afterwards. After closing both files, use std::remove() to
    delete the original file, and std::rename() to rename the temporary
    file to the original name.

    Both of the functions I mentioned are prototyped in <cstdio>.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 24, 2004
    #3
  4. Mahesha

    Jerry Coffin Guest

    (Mahesha) wrote in message news:<>...
    > Hello,
    > I'm new to C++ and I have requirement to open a existing text file in
    > write mode and write 2 new lines of text in the beginning of the file.
    > I'm working with fstream standard library.
    > If someone could direct me to a sample code to do this that would be
    > really helpful.


    Most file systems will only let you add to the end of a file. The
    usual way to handle this is:
    1. copy current file contents to a temporary file.
    2. rewind the original file.
    3. write your two lines to the file.
    4. copy the contents from the temporary file back to the original.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jul 24, 2004
    #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. nrm
    Replies:
    3
    Views:
    564
  2. Armando
    Replies:
    6
    Views:
    772
    Armando
    Jan 29, 2004
  3. Replies:
    1
    Views:
    358
  4. Writing to an existing file.

    , Aug 1, 2006, in forum: C Programming
    Replies:
    6
    Views:
    293
    Keith Thompson
    Aug 4, 2006
  5. Replies:
    0
    Views:
    208
Loading...

Share This Page