Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

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?

Thanks!

Jacky
0
wjacky
Asked:
wjacky
3 Solutions
 
cookreCommented:
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
 
gabbitaraviCommented:
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
 
drnickCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now