8 File Storage

Direct Access

Direct file access is used to store or retrieve data items from one logical record at a time.

The Direct PRINT# Statement

The direct PRINT# statement is nearly identical to the serial PRINT# statement except that it prints data onto the file starting at the beginning of a specified record. Syntax for this statement is as follows:

The data list is identical to that used in the serial PRINT# statement. The direct PRINT# statement prints data into the specified record of the file. Printing starts at the beginning of the specified record. Any previous data in the record is overwritten. Specifying END causes an EOF mark to be printed after the data (first syntax) or at the beginning of the record (second syntax). When END is not used, an EOR (end-of-record) mark is placed after the last item printed. The ON END# statement and the TYP function can be used to detect EOFs, as shown later in this chapter.

The program below prints consecutive numbers into each odd-numbered record of a 10-record file named TEN.

10   Data=1
20   ASSIGN #1 TO "TEN"
30   FOR Record=1 TO 10 STEP 2
40     PRINT #1,Record;Data
50     Data=Data+1
60   NEXT Record
70   END
By printing in specific records of the file TEN, previous data in those records is erased and replaced by the new data. File TEN now contains the following:

Comparison of Data Access Methods
Record No. Data
11 (EOR)
2(Null)
32 (EOR)
4(Null)
53 (EOR)
6(Null)
74 (EOR)
8(Null)
95(EOR)
10(Nothing)

An EOR is automatically added at the end of each odd-numbered record.

When neither the data list nor END are used in a direct PRINT# statement, it erases the contents of the specified record and fills it with EORs. For example, the following program erases every third record of file TEN, which was opened and accessed in the previous program:

100  ASSIGN #1 TO "TEN"
110  FOR Erase=1 TO 10 STEP 3
120    PRINT #1,Erase
130  NEXT Erase
140  END
The information now left in the file is as follows:

Comparison of Data Access Methods
Record No. Data
1(EORs)
2(Null)
32 (EOR)
4(Null)
53 (EOR)
6(Null)
7(EORs)
8(Null)
95(EOR)
10(Nothing)

When an EOR is detected by a serial READ# statement, it skips over the entire record and attempts to access data in the next record. You can use a direct PRINT# statement to write over the EOR marks.

When the data list is omitted from a PRINT# statement, as shown in the following statement, an EOF is placed at the beginning of the specified record:

PRINT# file number ,record ;END

If a serial or direct READ# attempts to read from that record, reading the EOF terminates the operation.

The Direct READ# Statement

The direct READ# statement is like the serial READ# statement except that reading of data into variables begins at the beginning of the specified record and will not go past an EOR mark. Like the serial READ# statement, the direct READ# statement will not read past an EOF mark. Syntax for the direct READ# statement is as follows:

READ# file number ,record number [;variable list]

As with serial READ# statements, the variables into which you read data items do not have to be the same variables used to print the data items on the record, but they must be the same type (numeric or string) and in the same order.

If the number of items making up the data list is greater than the data in the defined record, however, an EOR error occurs.

The following program reads the data printed in the 5th and 9th records of the previously-used file named TEN:

200  ASSIGN #1 TO "TEN"
210  READ #1,5;R5
220  READ #1,9;R9
230  PRINT "Data in record 5 =";R5
240  PRINT "Data in record 9 =";R9
250  END

Data in record 5 = 3
Data in record 9 = 5
The program reads the data from records 5 and 9 and outputs the data on the standard printer.

Repositioning the Record Pointer

If the variable list is omitted from a direct READ#, the pointer is repositioned to the beginning of the specified record. To reposition the pointer to the beginning of a file (for use with serial data access) execute the following:

READ# file number ,1


Eloquence Language Manual - 19 DEC 2002