Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to delete record from random access file

Status
Not open for further replies.

regor

IS-IT--Management
Jul 3, 2002
2
HK
I am having trouble deleting a record from a random access file..
I am left with an empty record.. ie. the number of records remains the same, one of them is blank though..

How do I remove the record ?
 
I guess the easiest thing to do is, take the parts that you want, put it into an array. KILL the file, and then remake it using only the parts that you wanted.
 
There is a far more efficient way to do this :) Just add a field to the record indicating whether it is in use or not. Then, to delete a record, simply mark it as deleted and rewrite just that record. When creating a new record, scan the file for a free record, and if you find one, overwrite it with one that is not deleted. You could also maintain a list of deleted records in another file, so that you don't have to read in all the existing records to find one that is deleted. The other thing you could do is have an extra 'dummy' record at the start of the file, and then in each record, store the record number of the "next free record". For allocated records, make this 0 or -1 or some other such special marker. Make that dummy first record point to the actual first free one. Then, when you delete one, set its pointer to the current "first free" one in that first record, and then set the first record to the newly freed block. When you then go to create a new record, you simply use the one pointed at by the first record. Kinda like this:
[tt]
TYPE recordType
nextFree AS INTEGER
data AS STRING * 30 'whatever data you want
END TYPE

SUB
deleteRecord(recordIndex%, fileNumber%)
DIM root AS recordType
DIM toDelete AS recordType

GET #fileNumber%, 1, root
GET #fileNumber%, recordIndex% * LEN(root) + 1, toDelete

toDelete.nextFree = root.nextFree
root.nextFree = recordIndex%

PUT #fileNumber%, 1, root
PUT #fileNumber%, recordIndex% * LEN(root) + 1, toDelete
END SUB

FUNCTION
allocateRecord%(fileNumber%)
DIM root AS recordType
DIM nextFree AS recordType

GET #fileNumber%, 1, root

IF (root.nextFree > 0) THEN
allocateRecord% = root.nextFree

GET #fileNumber, root.nextFree * LEN(root) + 1, nextFree
root.nextFree = nextFree.nextFree
PUT #fileNumber, 1, root
ELSE
allocateRecord% = LOF(fileNumber%) / LEN(root)
END IF
END FUNCTION

[/tt]
This example assumes that the "root" (dummy) node is index 0, and the first possible data node is index 1. [tt]allocateRecord%[/tt] returns the record index of the allocated record, and then you can fill in a [tt]recordType[/tt] variable and write it over that record, calculating the offset with: [tt]recordIndex% * LEN(recordTypeVariable) + 1[/tt]

As with most of the code I post here, this is of course untested :) Use at your own risk, some debugging may be required.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top