Disk File Input/Output |
It's not quite so simple to print or do calculations with numeric values from random I/O files, however, because they are represented as strings. You must convert each string that represents a number to its corresponding numeric value, either integer,
Assume that there is an additional
10 OPEN "R", #3, ":F1 :PERSON"
20FIELD #3, 20 AS N$, 9 AS SS$, 2 AS H$
30GET #3
40PRINT N$; SS$; "HOURS ="; CVI (H$)
50HW = HW + CVI (H$)
60PRINT "TOTAL HOURS ="; HW
The two characters defined as H$ are converted to an integer. To convert a string to a numeric variable, the number of string characters must equal the number of bytes required to store the corresponding numeric data type:
Integer | CVI | 2 bytes |
| CVS | 4 bytes |
| CVD | 8 bytes |
If the number of string characters represented by the argument of the function is fewer than required, an ILLEGAL FUNCTION CALL error message is printed and execution halts. If the string variable is longer than required, the extra characters are ignored.
Writing to a Random 1/0 File
To store data in a random disk file, you follow the same sequence of OPEN and FIELD statements as when reading from a random disk file. The PUT statement writes the 128 bytes of the I/O buffer to the disk file.
The FIELD statement, however, defines
For example, assume you have opened a file for random I/O, assigned it file number 3, and defined the first 20 characters as N$. You must use LSET or RSET to place values in the buffer:
10OPEN "R", #3, ":F1 :PERSON"
20FIELD #3, 20 AS N$
30LSET N$ = A$
40PUT #3,1
This technique may be used for storing variables for use in another program, linked to the first program with the RUN statement (see description of RUN, chapter 6).
Whatever the length of A$, its leftmost 20 characters are now identified as N$. If A$ is less than 20 characters long, the remaining characters of N are set to ASCII blanks (20H). Statement 40 writes all 128 bytes of the I/O buffer to disk file number #3.