• Keine Ergebnisse gefunden

THE FILE I/O STATEMENTS

Im Dokument BASIC Language Reference vs (Seite 143-146)

CHAPTER 3 DATA FORMATS

99 BOTTLES OF BEER ON THE WALL

8.4 THE FILE I/O STATEMENTS

Transfer of data between VS BASIC programs and files is performed by five statements: READ, GET, WRITE, PUT, and REWRITE. Records in consecutive files can be read selectively by position in the file by using the SKIP statement before a READ. You can use the DELETE statement to remove selected records from an indexed file. READ,

WRITE, REWRITE, SKIP, and DELETE operations can be performed on a file only while it is open (i.e., after an OPEN statement is executed for that file, and before a CLOSE). This section describes the way in which data is transferred between the file and the record area, and between the record area and program data. For the general forms and full discussions of the various optional clauses and modes of use for these statements, refer to the appropriate entries in Part II of this manual.

8.4.1 The READ Statement

The READ statement causes one record to be read from the specified file into the record area for that file. You can use READ with or without a list of receivers. If a list of receivers is included in the READ statement, values are extracted one by one from the record area and assigned to the receivers, left to right. If USING is

specified, the values are assigned according to the formats specified in the referenced FMT or Image (%) statement (refer to Section 7.4 and the FMT and Image (%) entries in Part II). Otherwise, values are asstuned to be in internal format. If no list of receivers is present, one record is simply read from the file to the record area, and no assignments are performed. Once in the record area, a record is available to the GET, WRITE, and REWRITE statements.

If the file being read is consecutive, you can use the RECORDS

=

n

clause to specify that the nth record of the file is to be read. If a READ statement on a consecutive file does not have the RECORDS = n clause, the next sequential record is read. For indexed files, use the KEY clause to read a record with a primary or alternate key equal to a particular value. Refer to Part II of this manual for further details on the READ statement.

8.4.2 The GET Statement

The GET statement causes values to be extracted from the record area and assigned to one or more receivers. Values are extracted from the record area and assigned according to the format in the FMT or Image (%) statement (refer to Section 7.4 and the FMT and Image (%) entries in Part II) referenced with the USING clause, if one is present. If USING is not specified, values are assigned according to the

conventions of VS BASIC's internal format. GET is generally used to assign values to receivers after a record is read from a file by a READ statement without a receiver list. If a PUT, WRITE, or REWRITE statement was executed more recently than the last READ, however, the record area contains whatever record was left there by the most recent of these statements. Refer to Part II for further details.

8.4.3 The WRITE Statement

The WRITE statement causes one data record to be written from the record area to the disk file. You can use WRITE with or without a list of expressions. If a list of expressions is included in the WRITE statement, their values are first packed into the record area.

Note that an expression in a WRITE statement cannot contain a

concatenation operation. If USING is specified, the values are packed into the record area according to the format in the referenced FMT or Image (%) statement (refer to Section 7.4 and the FMT and Image (%) entries in Part II). If USING is not specified, the values are packed into the record area according to the conventions of VS BASIC's

internal format. The contents of the record area are then written to the specified file.

If the WRITE statement contains no list of arguments, the current contents of the record area are written to the file. Generally, this form of the WRITE statement is used after data are written into the record area by a PUT statement. If a READ, WRITE, or REWRITE

statement was executed more recently than the last PUT, however, the record area contains whatever was left there by the most recent of these statements.

Records written to consecutive files (in Output, Shared, or Extend mode, as specified in the OPEN statement) are added to the end of the file. Records written to indexed files (in I/O mode) are inserted

into the file at the appropriate point as determined by their primary key values. Refer to Part II for further details.

8.4.4 The PUT Statement

The PUT statement causes the values of one or more expressions to be packed into the record area of the specified file. If USING is

specified, the values are packed into the record area according to the format in the referenced FMT or Image (%) statement (refer to Section 7.4 and the FMT and Image (%) entries in Part II). If USING is not specified, the values are packed into the record area according to the conventions of VS BASIC's internal format. PUT is generally used prior to a WRITE 1Statement with no argument list.

8.4.5 The REWRITE Statement

REWRITE is like WRITE except that the record that is written to the file overwrites the last record read with a HOLD option, instead of being written to the end of a file. For a description of the HOLD option, refer to the entry under READ in Part II. REWRITE cannot be performed on consecutive files with variable-length records. Direct concatenation operations within the REWRITE statement are illegal.

8.4.6 Summary of Data Flow Controlled by File I/O Statements

Figure 8-2 illustrates the transfer of data for the READ, GET, WRITE, REWRITE, and PUT statements.

Program Variables or Expressions

Record Area

( OMS ) (Buffer)

Disk or Tape File READ X

(<----

optional ---) X

<---

(X)

<---

X IGET

x <--- x

(RE)WRITE X (--- optional

---->)

X

--->

(X)

--->

X

PUT

x ---> x

Figure 8-2. Statement-Dependent Data Transfer Paths

8.4.7 Data Representation in File I/O

In using file I/O statements, it is important to keep in mind that VS BASIC represents numeric data in an internal format that bears no simple relation to the sequences of ASCII character codes used to represent that same data in workstation or printer I/O. Unless file I/O is explicitly formatted with the USING clause and Image (%) or FMT statements, all file I/O is performed in this internal format. While this is suitable for data files that are to be read only by other programs, do not use it for files to be directly examined by users, such as report or other text files. Any attempt to display or print numeric data from a file in internal format produces meaningless strings of characters on the workstation or printer.

Data values packed into records in internal format take up the following amounts of space:

•Floating-point

•Integer

•Alphanumeric

8 bytes 4 bytes

defined length

If any format conversions are done (i.e., if USING is specified in any file I/O statement), they are performed as data is transferred between the record area and variables or expressions in the program. Always read data from a file in the same format in which it was written for it to be properly interpreted by a VS BASIC program.

For a discussion of the FMT and Image (%) statements, refer to Section 7.4 and the FMT and Image (%) statement entries in Part II.

Im Dokument BASIC Language Reference vs (Seite 143-146)