IB Statements/read

From CometWiki

(Difference between revisions)
Jump to: navigation, search
Badge (Talk | contribs)
(New page: '''READ statement''' '''Syntax:''' READ (lun, format-statement-label) [KEY=index] [,EXCP=statement-label] '''Discussion:''' The READ statement inputs a physical record from a keyed ...)
Newer edit →

Revision as of 10:13, 20 May 2009

READ statement

Syntax: READ (lun, format-statement-label) [KEY=index] [,EXCP=statement-label]

Discussion: The READ statement inputs a physical record from a keyed or indexed sequential data file into variables within the user program.

The format-statement-label parameter refers to the FORMAT statement that defines the fields in the data record. These fields are listed in record order in the FORMAT statement (see Example 1 below).

Successful execution of a READ statement automatically advances the file pointer to the next available record in key-sequential order.

Reading a keyed file may be done with a string index, a numeric index, or no index. With a string index, Comet will read the data record associated with the supplied key (index) value. With a numeric index, Comet will read the records in record number order without regard for the key tree. With no index, Comet will read the file in key-sequential order.

Reading an indexed sequential file may be done with a numeric index or no index. With a numeric index, Comet will read the record matching the record number position specified. With no index, Comet will read the records in sequential order.

Example 1: 1000 FORMAT CUSTNUM$;NAME$;ADDRESS$;CITY$;STATE$;ZIP$

.

OPEN (1) "CUSTFILE"

.

INPUT (0) CUSTNUM$

IF CUSTNUM$ = "" THEN CLOSE (1) & RUN "QMONITOR"

READ (1,1000) KEY=CUSTNUM$, EXCP=9999

.

9999 IF EXCP=32 THEN PRINT (0) "KEY NOT FOUND" & GOTO 100

       IF EXCP=33 THEN PRINT (0) "RECORD LOCKED" & GOTO 100
       ERROR

In this example, a keyed file named CUSTFILE is opened. The user inputs a specific customer number into the variable named CUSTNUM$, and the program reads the data file using this value as an index. If an exception occurs, the program branches to statement label 9999, where the system variable named EXCP is checked for the most probable exceptions (32 is the Comet exception for "KEY NOT FOUND" and 33 is the Comet exception for "RECORD UNAVAILABLE -- EXTRACT ERROR"). If the exception is the result of something other than these two, the program comes to an abnormal end (ERROR).

Example 2: INPUT (0) VALUE

READ (1,1000) KEY=VALUE, EXCP=9999

This example shows how to read a record using a numeric index.

Example 3: READ (1,1000) EXCP=9999

This example shows how to read a record in sequential or key-sequential order.

Personal tools