BASIC-SO

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, single-precision floating-point, or double-precision floating point. The CVI, CVS, and CVD functions, respectively, perform this conversion.

Assume that there is an additional 2-character field following the 9-character social security number that represents the number of hours worked in the previous week. The following statements open the file, define the fields, read the first (or next) record, and increment the variable HW by the number of hours worked:

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

Single-precision value

CVS

4 bytes

Double-precision value

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 fixed-length fields in the I/O buffer. To get the data to be written to disk into the I/O buffer, there are two special assignment statements: LSET and RSET. LSET left-aligns a variable in the buffer field, and RSET right-aligns it. In both cases, if the variable is shorter than the field, it is pad- ded with ASCII blanks (20H); if the variable is longer than the field, it is truncated.

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.

5-7

Page 39
Image 39
Intel 9800758-02 manual Writing to a Random 1/0 File, Integer, Single-precision value, Double-precision value