By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,509 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,509 IT Pros & Developers. It's quick & easy.

delete from a file, maybe overwrite!?

P: n/a
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::out);

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???
Oct 2 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Someonekicked" <so***********@comcast.net> wrote in message
news:4b********************@comcast.com...
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::out);

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
Oct 3 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.