8 File Storage
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 ENDBy 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:
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 ENDThe information now left in the file is as follows:
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.
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 = 5The program reads the data from records 5 and 9 and outputs the data on the standard printer.
READ# file number ,1