delete from a file, maybe overwrite!?

Discussion in 'C++' started by Someonekicked, Oct 2, 2005.

  1. I have a binary file, and I need to delete a specific number of characters
    in it.
    The file contains records, each record has a specific length.
    So the way I wanna handle deleting is that I will overwrite the record I
    want to delete with the last record in the file, then actually delete that
    last record in the file. the problem Im having is how to delete that last
    record in the file.

    Suppose each record is of length 43, then I have something like:

    fstream inoutData;

    inoutData.open("this_file.txt", ios::binary | ios::in | ios::eek:ut);

    char read_this[44];

    inoutData.seekg(-43,ios::end);

    inoutData.read(read_this,43);

    read_this[43] = '\0'; // now read_this contains the last record

    inoutData.seekp(-43,ios::end);

    inoutData.write("",43); // << here, how I can delete the last record???
     
    Someonekicked, Oct 2, 2005
    #1
    1. Advertising

  2. Someonekicked

    lallous Guest

    "Someonekicked" <> wrote in message
    news:...
    >I have a binary file, and I need to delete a specific number of characters
    >in it.
    > The file contains records, each record has a specific length.
    > So the way I wanna handle deleting is that I will overwrite the record I
    > want to delete with the last record in the file, then actually delete that
    > last record in the file. the problem Im having is how to delete that last
    > record in the file.
    >
    > Suppose each record is of length 43, then I have something like:
    >
    > fstream inoutData;
    >
    > inoutData.open("this_file.txt", ios::binary | ios::in | ios::eek:ut);
    >
    > char read_this[44];
    >
    > inoutData.seekg(-43,ios::end);
    >
    > inoutData.read(read_this,43);
    >
    > read_this[43] = '\0'; // now read_this contains the last record
    >
    > inoutData.seekp(-43,ios::end);
    >
    > inoutData.write("",43); // << here, how I can delete the last record???
    >
    >


    Hello

    To delete the last record is equivalent to setting a new end-of-file. AFAIK
    this is OS related and non standard.

    If you were using MS Windows API, I would suggest looking @ SetEndOfFile().

    Now in your case, if your records all have the same size, you can instead of
    delete a record, you simply mark the record as free or as deleted, then
    whenever you want to add, you search for that marker and write new record
    there.

    Hope that helps,
    Elias
     
    lallous, Oct 3, 2005
    #2
    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. John Harrison

    Re: file overwrite data manipulation

    John Harrison, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    5,958
    John Harrison
    Jun 27, 2003
  2. Chris Theis
    Replies:
    0
    Views:
    810
    Chris Theis
    Jun 26, 2003
  3. Guest
    Replies:
    5
    Views:
    622
  4. Ville Vainio
    Replies:
    11
    Views:
    617
    Hamish Lawson
    Aug 10, 2004
  5. ma740988
    Replies:
    1
    Views:
    483
    Kai-Uwe Bux
    Mar 6, 2006
Loading...

Share This Page