OPEN Statement

The OPEN statement is necessary to access a file in the current program.

Format

OPEN ['DICT',] file [TO filevar] THEN/ELSE statements

Parameter(s)

'DICT'

Opens the file dictionary. If the dictionary is not specified, the data file is assumed.

file

An expression evaluating to the file name to be opened. If the file is one of several data files associated with a single file dictionary, it may be opened by the syntax, 'dictname,file', with dictname the name of the file dictionary.

TO filevar

Defines filevar as the file variable name by which the file is accessed. If the TO filevar clause is not specified, the file may only be accessed as the default file variable.

THEN statements

Executes statements if filename is opened successfully.

ELSE statements

Executes statements if filename cannot be opened. This clause is generally used to cause print error messages or to stop or abort the program.

Description

The OPEN statement prepares a file for use by the current mvBASIC program. All references to a file within an mvBASIC program must be preceded by a separate OPEN statement for that file.

If a file variable is not assigned with the TO keyword, the file is assigned to the default file variable. Any subsequent file I/O statements that do not specify a file variable default to this file. Note that default file variables are not local to the program from which they are executed; when a subroutine is called, the current default file variable is shared with the calling program.

There is no limit to the number of files which can be open at a given time. However, if multiple files are opened and accessed concurrently, file variables must be used. The default file variable can represent only one file at a time.

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 programs abort. A READ statement is then used to find the file item. If the item is found, any current reservations is shown; if not, 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