Deleting data from the file without deleting the file first

Discussion in 'C++' started by crea, Dec 27, 2012.

  1. crea

    crea Guest

    This is a simple question, but I have never found an answer to this and been
    thinking about it. Plus not sure if this is the right forum, but not sure
    where to put this...I guess some kind of general programming guestion.

    So we open a file with iofstream and then write /save some text to file.
    Lets say we save a text:

    "Hello World!!"

    To a file HW.txt.

    Now, is it possible to delete the character 'o' from the file without
    deleting the file HW.txt first?
    Currently I think the only way to do this is to read the whole data from
    file to a string and then delete it from the string and then save the whole
    string to the file.
    crea, Dec 27, 2012
    #1
    1. Advertising

  2. On Thu, 27 Dec 2012 07:58:28 +0000, crea wrote:

    > Lets say we save a text:
    >
    > "Hello World!!"
    >
    > To a file HW.txt.
    >
    > Now, is it possible to delete the character 'o' from the file without
    > deleting the file HW.txt first?


    That depends on what you mean with 'delete the file'. You have to rewrite
    the entire file, but you don't have to tell the filesystem that the file
    is gone before you write it anew.

    > Currently I think the only way to do this is to read the whole data from
    > file to a string and then delete it from the string and then save the
    > whole string to the file.


    Yes, that is correct. Note that those steps don't include removing the
    file from the filesystem. When you open the file for writing/saving, you
    just have to use ios::eek:ut|ios::trunc to ensure the old contents are gone.

    Bart v Ingen Schenau
    Bart van Ingen Schenau, Dec 27, 2012
    #2
    1. Advertising

  3. crea

    Nobody Guest

    On Thu, 27 Dec 2012 07:58:28 +0000, crea wrote:

    > So we open a file with iofstream and then write /save some text to file.
    > Lets say we save a text:
    >
    > "Hello World!!"
    >
    > To a file HW.txt.
    >
    > Now, is it possible to delete the character 'o' from the file without
    > deleting the file HW.txt first?


    Using only the features of the standard C or C++ libraries, the best you
    can do is to shift everything after the "o" back one space, by reading it
    in then writing it out at a different offset.

    Some operating systems may have the ability to delete chunks from the
    middle of the file, but most don't, and the standard library doesn't
    provide any mechanism to make use of such a feature.
    Nobody, Dec 28, 2012
    #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. Dave
    Replies:
    3
    Views:
    292
    Denis Remezov
    May 13, 2004
  2. Harry Barker
    Replies:
    2
    Views:
    503
    Alf P. Steinbach
    Apr 19, 2006
  3. pozz

    Deleting first N lines from a text file

    pozz, Nov 15, 2011, in forum: C Programming
    Replies:
    26
    Views:
    1,938
    Dr Nick
    Dec 16, 2011
  4. suresh
    Replies:
    15
    Views:
    212
    Jimmy Kofler
    Jun 5, 2008
  5. Richard Schneeman
    Replies:
    16
    Views:
    452
    Daniel Bush
    Aug 27, 2008
Loading...

Share This Page