The rest of this article assumes you have typed in the program. If there were no errors In typing, you should now have 3 options on the screen. First we must create a data file so type 1. This will send the program to line 100. We will no", create a 100 record file to work with. Each record will contain a record number, an item count, and an item description field. Each field is separated by a comma. As the file is created it will be displayed on the screen. After record 100 is written, the file is closed and you will be returned to the 3 original options.
Now, type the number 2. In order to use random access updating. We must know exactly where each record begins on the diskette. Before Updating, the program will create an index using an array in memory. Once this is done, we can instantly find any record using the point instruction. But first, we must note the location by storing sector and byte in our arrays. We only have to do this once. Then we can inspect or change as many records as needed.
The index is created using the routine starting at line 300 and ending at 420. At line 500 is the beginning of the random access routine. You should be able to follow the program listing since the variables used will all be defined at the end of this article. At this time, I will only explain how the note and point instructions are used.
At line 310, we check a flag to see if an index has already been created If so, we do tot have to repeat this procedure and go to line 500. To create the index, we read the datafile Before r eading each record, we note the sector and byte position and put it into our SEC and BYT arrays
Once the array is complete we ate ready to display or update any record in the datafile using the point instruction to locate the record we want Let's start by displaying record 50. Type 1) and press [RETURN]
Then type 50 and press [RETURN]. At line 760, we point to the sector and byte of record 50. At line 780, we input that record, clear the screen, and display record 50 on the screen.
Remember the number of items in this record, then press and key and you will be returned to the option routine at line 5000. Type 2 and this time we will change record 5 0. Type U and press [RETURN] then type 50 and press [RETURN] Record 50 will again be displayed but now we have 3 new options. Let's take them in order. Type 1 then press [RETURN] To update the quantity, we merely add to it by typing the number of items to add, or subtract by typing a negative number. Remember that our quantity field is only 3 positions so don't increase it to more than 999 items.
When we read the record from the datafile, we stored in a string call REC$. The quantity field has just been updated in the string only. It will be updated on the disk only when we choose option 3 to exit. Before we exit, let's change the description field. Type 2 and press [RETURN] Choose a new description and type it. Now type 3 and the record will be updated on the disk.
To be sure that the record has been changed properly, you can choose the display/update option then redisplay record 50. By now you see the advantages of random access updating. You don't have to read the first 49 records to get to record 50. Once the arrays of the sectors and bytes contain the beginning of every record, we can locate any record instantly.