469,315 Members | 1,609 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

delete from a file, maybe overwrite!?

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
1 4029
"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.

Similar topics

2 posts views Thread by andy | last post: by
2 posts views Thread by Paul LAURENT | last post: by
8 posts views Thread by Dong Ge | last post: by
6 posts views Thread by Poppy | last post: by
1 post views Thread by Kyote | last post: by
26 posts views Thread by Army1987 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.