Solved

implementing deletion in a binary file

Posted on 2003-11-24
6
291 Views
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?

Thanks!

Jacky
0
Comment
Question by:wjacky
6 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 43 total points
Comment Utility
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:
http://www.wotsit.org/download.asp?f=ti838d
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?
0
 
LVL 1

Assisted Solution

by:gabbitaravi
gabbitaravi earned 41 total points
Comment Utility
Hi,
 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 (binaryfile.read()!=EOF)
{
         if((strcmp(a.name == name)) && (a.age  == age)...)
         {
          skip()// do not write into new file
         }
        else
       {
        writeintoNewFile();
       }
}
binaryfile.close();
writetoNewFile.close();

with warm regards,
ravi kiran
0
 
LVL 5

Assisted Solution

by:drnick
drnick earned 41 total points
Comment Utility
hi,

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.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now