Solved

implementing deletion in a binary file

Posted on 2003-11-24
6
293 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
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:
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
ID: 9815783
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
ID: 9823053
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented javascript web form 8 99
splitOdd10 challenge 5 74
allswap challenge 6 75
Beginner to Unreal Engine 4 5 57
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

932 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

11 Experts available now in Live!

Get 1:1 Help Now