READ

Function

Reads the next record of a file into the specified variables.

Syntax

READ {filenum_lit|_var|_col} INTO {any_var:length_int_lit|_var|_col}...
[STATUS=status_num_var]

Arguments

filenum_lit|_var|_col

Number assigned in OPEN to the file to be read.

any_var:length_int_lit|_var|_col

One or more variables into which data from the record read are to be put. length_int_lit|_var|_col specifies the length of each field of data.

STATUS

Optional variable into which a read status is returned.

Description

Text and binary data is parsed according to the following:

The total length indicated for the variables must be less than or equal to the length of the record being read.

If there are no more records to read, the #end-file reserved variable is set to 1; otherwise, it is set to 0 (zero). Your program should check this variable after each READ command.

If STATUS is specified, Production Reporting returns 0 if the read is successful; otherwise, it returns the value of errno, which is system-dependent.

Examples

The following example shows several READ commands:

read  1  into  $name:30  $addr:30  $city:20  $state:2  $zip:5
read  3  into  $type:2  #amount:2  #rate:1  $code:5  $date:11
read  #j into  #sequence:2  $name:20  $title:15

The following example shows a READ command that reads two dates. One is loaded into a date variable; the other is loaded into a string variable, which is then converted to a date using the strtodate function.

.
.
.
declare-variable
  date $date1 $date2
  text $text
end-declare
.
.
.
read 4 into $date1:18 $text1:18
let $date2 = strtodate($text1,'SYYYYMMDDHHMISSNNN')
or
let $date2 = strtodate($text1)

The following example shows a READ command with an INSERT loop:

begin-sql
  begin transaction
end-sql
while  1    ! Infinite loop, exited by BREAK, below.
  read  10  into  $company:40  $parent:30  $location:50
  if #end-file
    break   ! End of file reached.
  end-if
  begin-sql
    insert into comps (name, parent, location)
      values ($company, $parent, $location)
  end-sql
  add 1 to #inserts
  if #inserts >= 100
    begin-sql
      end transaction;
      begin transaction
    end-sql
  move 0 to #inserts
  end-if
end-while
begin-sql
 end transaction
end-sql

See Also

OPEN, CLOSE, and WRITE for information on files