Section 6.2
6 Files
6.2 Serial & Sequential Files
Records are placed onto the disk or tape one after the other with no regard for
sequence.
Transaction files are stored serially.
Records are stored one after another in a recogniable order.
The Primary Key
One field is chosen by which records are ordered.
6.2.3 Creating Serial And Sequential Files
The three QuickBASIC commands that we need to create serial or sequential files
are:
The OPEN
Command
OPEN Filename$ FOR OUTPUT AS #n
This creates a file called Filename$
so data from the program can
be output to it.
n%
is an integer, representing the channel number, between 1 and
255.
The WRITE
Statement
WRITE #n, <variable>, <variable>, etc.
n%
must be the same number used in the OPEN
statement.
Each of the variable name after the WRITE
statement will create
consecutive fields in a record.
The CLOSE
Statement
CLOSE #n
This closes the specified channel.
6.2.4 Reading From Serial And Sequential Files
In order to read data from such a file we need to:
OPEN Filename$ FOR INPUT AS #n%
INPUT #n%, <variable>, <variable>, <variable>, etc.
CLOSE #n%
Note that data is always input starting from the beginning of the file.
The variables and their data types should match with those used in the
WRITE
statement.
6.2.5 Appending Records To A Serial File
As records in a serial file are in no particular order to add a new record we
can simply append it to the end of an existing file.
In order to do this we need to:
OPEN Filename$ FOR APPEND AS #n%
WRITE #n, <variable>, <variable>, etc.
6.2.6 Deleting Records From A Serial Or Sequential File
In order to delete a record, a brand new file (of the same name) has to be
created without the record that was to be deleted.
There are two methods of doing this:
Physical Deletion
Open a channel for input from the file.
Open another channel for output to a new file name.
Input a record from the old file and check if it needs to be deleted.
If it is to be kept, write it to the new file. If it is not, then move on to the
next record.
Close the channels.
Delete the old file and rename the new file as the old one.
Logical Deletion
Each record is given an extra field. This field is a flag field. If it is equal
to 0 then the record is shown as existing. If it is equal to 1 then, although the
record exists, it is not shown and can be considered to be logically deleted.
6.2.7 Using 2D Arrays
All data (textual or numeric) can be represented as string data.
It is a more compact way of programming to swap many 1D arrays for one 2D
array.
For example:
DIM Name$(1 TO 4)
DIM Age%(1 TO 4)
could be stored in a 2D array dimensioned as:
DIM NameAge$(1 TO 2, 1 TO 4)
The elements of the array are then:
Names: NameAge(1, 1)
through to NameAge$(1, 4)
Ages: NameAge(2, 1)
through to NameAge$(2, 4)
In General: NameAge$(Field, Record)
6.2.8 Working With A Variable Number Of Records
There are two ways to handle a situation involving a variable number of records.
Use The End Of File (EOF) Function
At the end of serial and sequential files we can detect the end of file marker.
This can be done by examing the value of:
EOF(n)
where n is the channel number.
At the end of the file EOF(n) = -1
otherwise EOF(n) = 0
.
Use A Reference File
When a serial file is created a separate reference file of the same name but with the
extension 'tot' is also created.
If records are added or deleted then the total in the reference file has to be altered.
This is done by:
- Inputting the old total.
- Adding or subtracting the appropriate number of records.
- Writing out the new total over the old total.
6.2.9 Adding Records To A Sequential File
Unlike serial files we cannot append new records to the end of the file. New records
are added using a method known as Updating by Copying.
This is done by inputting records one-by-one and then writing each record one-by-one
making sure that the new record is inserted in the right place.
Pseudocode
INPUT the new details
OPEN old file FOR INPUT
OPEN new file FOR OUTPUT
DO UNTIL end of old file
    INPUT a record from old file
    IF key of new record is earlier then key of current record THEN
       write new record to new file
    END IF
    write out current record to new file
LOOP
If new record has not been added then add it now
CLOSE files
rename files
END
Updating A Master File
In the business world it is common to 'update' a file by changing more than one record
at a time. A file can be updated by:
- having records added
- having records modified
Example
An electricity company's master file contains records for customers. New customers
have to be added and customers who move house need to have their records modified.
In order to do this we need to have three files:
- Master file
- Transaction file
- New master file
The master file could (for example) contain names, address and account numbers for
customers. The transaction contains records that need to be added and records that
need to be modified. The new master file will be produced by processing the
transaction file on the existing master file.
These are the steps that we would need to go through:
- Make sure the transaction file is in the same sequence as the master
file.
- Read a transaction record into main memory.
- Read a master record into main memory.
- If the transaction record is less than the master record, write to the
new master file, read in the next transaction record and go to 4 (this is
recursive).
- Write the master record to the new file.
- Go to 3.
If we want to modify existig records as well then this could be incorpriated in
to step 4.
We may wish to keep the old master file as a record of previous situations.
6.2.10 Working With An Unknown Number Of Fields
Information can be retrieved from a file's record at the time using:
LINE INPUT #n, rec$
This reads all the characters including commas to the next end of record marker
and assigns the string to rec$
.
The string handling functions can then be used to extract the field data.