implementing deletion in a binary file

Posted on 2003-11-24
Last Modified: 2010-04-17
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?


Question by:wjacky
LVL 22

Accepted Solution

cookre earned 43 total points
ID: 9815765
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?

Assisted Solution

gabbitaravi earned 41 total points
ID: 9815783
 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

Assisted Solution

drnick earned 41 total points
ID: 9823053

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.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is an explanation of a simple data model to help parse a JSON feed
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question