implementing deletion in a binary file

Hi there,

does anybody know how to implement the deletion in a binary file, in which, I don't wanna copy the records I need to a new file, and replace to itself.

just like the MS Access, I was thinking if they are doing anything like duplicating the file, and then delete the records.

any ideas?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It's not entirely clear to me what you're getting at.

A file is just a collection of bits that has no inherrent meaning or structure.  Rather, structure is presumed by programs that fiddle with said bits.  As such, indiscriminant piddling gains nothing - you must know how a file is organized to get any use out of it.

Database files in particular have fairly complex internal structures that don't readily lend themselves to easy manipulation.  For example, this link:
gets you a 400 some odd line description of the old dBase DBF file format.  Once you figured it out completely, you could probably write some code to mark records as deleted.  You could then write more complex code to actually remove them from the DB (PACK).  Of course, if there were any related indices (IDX files), you'd have to fiddle with them too.

Access MDBs are more complex and come in several incompatable flavors.

Oracle is worse.

Or have I completely missed the point?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 In a binary file data will be in the form of Structures. If u want to implement Deleting option, U need to read the data line by line from binary file into a temp structure variable and compare the values with those that u want to delete and finally rewrite the remaining values into another file.

The Procedure is as follows
structure a;
while (!=EOF)
         if((strcmp( == name)) && (a.age  == age)...)
          skip()// do not write into new file

with warm regards,
ravi kiran

if you want to delete data of a specific size at a specified position in a binary file, you can do the following
with two buffers and no second file.
this is not how this is done in databases.
there, they just mark the space of the data to be deleted as "free" and use it sometimes again.
this is faster, but it leaves an overhead of unused space plus you need a free-space-list somewhere in your file.
however, if you really want to delete stuff phsically out of your file, the following method can be a good choice.
it assumes that you know where the data you want is situated and how big it is, which is, i think, is normally the case

i' gonna demonstrate this at an example

imagine, you file contains the bytes

[0 1 2 3 4 5 6 7 8 9 a b c d e f]

you want to delete 3 bytes at position 5 (that would be [5 6 7].
let us also imagine, due to memory restrictions you two buffers are 2 bytes large each

buffer1 [x x]    buffer2 [x x]
fill1 = 0           fill2 = 0
rp = 8             wp = 5

first, you go to the position you want to delete + size you want to delete and read
as much data you can in buffer1 and increase rp by the size you read
in our case, it would be 2 bytes:

buffer1 [8 9]    buffer2 [x x]
fill1 = 2           fill2 = 0
rp   = 10         wp = 5

LOOPSTART: now you start a loop:

copy buffer1 to buffer2 (or swap pointers and fill-sizes, would be faster)

buffer1 [8 9]    buffer2 [8 9]
fill1 = 2           fill2 = 2
rp   = 10         wp = 5

now you write fill2 bytes from buffer2 at wp

[0 1 2 3 4 8 9 7 8 9 a b c d e f g]

and increase wp by the count of bytes you've written

next you read as much bytes as you can at rp in your buffer1, it would again be 2, and increase readposition

buffer1 [a b]         buffer2 [8 9]
fill1 = 2                fill2 = 2
rp   = _12_           wp = _7_
go to LOOPSTART until you've read 0 bytes (reached end of file)

after the end of the loop, you set the endoffile simple at position wp.
in windows, you can use FileSeek and SetEndOfFile or such

the loops will produce

[0 1 2 3 4 8 9 a b 9 a b c d e f g]
buffer1 [c d]         buffer2 [a b]
fill1 = 2                fill2 = 2
rp   = _14_           wp = _9_

[0 1 2 3 4 8 9 a b c d b c d e f g]
buffer1 [e f]         buffer2 [c d]
fill1 = 2                fill2 = 2
rp   = _15_           wp = _11_

[0 1 2 3 4 8 9 a b c d e f d e f g]
buffer1 [g x]         buffer2 [e f]
fill1 = 1                fill2 = 2
rp   = _16_           wp = _13_

[0 1 2 3 4 8 9 a b c d e f g e f g]
buffer1 [x x]         buffer2 [g x]
fill1 = 0                fill2 = 1
rp   = _17_           wp = _14_

set end of file:

[0 1 2 3 4 8 9 a b c d e f g]

and voila, 3 bytes deleted and just a minimum of reading/writing work

also, in windows for example, it is possible to use asynchronous file operations,
allowing you to write a block and read the next block simultaneously.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.