READ Statement

The READ statement assigns the string value of a file item to a variable.

Format

READ var FROM [filevar,] item-ID [THEN statements1] [ELSE statements2]

Parameter(s)

var

Assigns var to the string value of the file item, in dynamic array form.

FROM [filevar,] item-ID

Item-ID is an expression evaluating to an item-ID. Assign var the string value of item-ID in the file which was opened as filevar. If filevar is not specified, the default file variable is used, which is the file most recently opened without a file variable assignment. If the specified item-ID does not exist, var is assigned the value of the null string ("").

THEN statements1

Executes statements1 if item-ID is read successfully.

ELSE statements2

Executes statements2 if item-ID cannot be read.

Description

Before a file can be accessed in a READ statement, it must be opened with an OPEN statement or an error will occur at runtime. See OPEN Statement for more information.

In mvBASIC there are also READU, READV, and READVU statements available. The READU statement sets an item update lock on the file item before reading it, the READV statement reads a single attribute from a given file item, and the READVU statement sets a item update lock and then performs a READV. See READU Statement, READV Statement and READVU Statement for more information.

Example

In this application, the OPEN statement is used to open a reservation file and the operator is asked to enter the customer's last name, to be used as an item-ID. If the reservation file is not found, the program aborts. A READ statement is then used to find the file item. If the item is found, any current reservations are shown; if it is not found, a new reservation may be entered.

OPEN "RESERVATIONS" TO RES.FILE ELSE   

   ABORT 201,"RESERVATIONS"    

END

   .

   .

   .

LOOP   

   PRINT "LAST NAME :  " :

   INPUT ITEM.ID

   READ RECORD FROM RES.FILE,ITEM.ID THEN

      PRINT ITEM.ID : " ON FILE."

      GOSUB SHOW.RES

   END ELSE

      PRINT ITEM.ID : " NOT ON FILE"    

      GOSUB ENTER.RES

   END

UNTIL LAST.NAME = "" DO REPEAT

See Also

Statement and Function Reference