delete from a file, maybe overwrite!?

S

Someonekicked

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???
 
L

lallous

Someonekicked said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top