Reading and Updating File Items

Before an item in a file can be accessed, it must be assigned a symbolic name, called a file variable. The file variable is necessary to provide a pointer to the file that will be used by the program each time the file is accessed.

File Variables (OPEN)

The OPEN statement assigns a file variable to a file, so that the program can read, write, select, or delete items in the file. All subsequent access of the file must reference the file variable and not the file name itself.

If a file is opened without a file variable specified, it uses the default file variable. Any subsequent file access statements that do not specify a file variable will use the default file variable. Only one file can be assigned to the default file variable at a single time.

Reading and Writing a File Item (READ, WRITE, etc.)

Once the file is opened, any item can be directly accessed. The READ statement assigns the string value of a file item to a dynamic array variable. The fields of the array can then be accessed by the dynamic array processing functions EXTRACT, REPLACE, INSERT, and DELETE. The WRITE statement writes a new or updated dynamic array string into a file item. There are several variations to READ and WRITE provided by mvBASIC. The READV and WRITEV statements read and write only a single attribute of an item, as a shortcut for programs which are concerned only with a single attribute. In addition, the MATREAD and MATWRITE statements read and write items as dimensioned arrays, with each attribute corresponding to an element of the array.

The file-reading statements are each equipped with THEN and ELSE clauses. If the item cannot be found, the v statements are executed. If it can be found, the THEN statements are executed. See Internal Program Control for more information on the syntax of THEN and ELSE clauses.

File Item Locks (READU, WRITEU, RELEASE, etc.)

Each of the statements for reading a file item have corresponding statements that place a lock on the file item as it is read. These statements are the READU, READVU, and MATREADU statements. (The U suffix stands for Update, declaring that the file item might be changed and rewritten.) The item lock is lifted either when the item is released with a RELEASE statement, deleted with a DELETE statement, written with a WRITE, WRITEV, or MATWRITE statement, or when the program is terminated. Until the lock is lifted, no other users will be able to access the same file item with a READU, READVU, or MATREADU statement.

File item locks only affect other READU, READVU, and MATREADU statements. While an item is locked, programs can access the file item with a normal READ, READV, or MATREAD statement, or they can even write it with any of the file writing statements. The success of a file item lock depends on its being respected by all other programs that access the same file.

If an item is to be written but the programmer does not want the lock removed, the WRITEU, WRITEVU, and MATWRITEU statements should be used in place of WRITE, WRITEV or MATWRITE. These statements will write the file item but retain the item lock for subsequent update. (Again, the U suffix stands for Update, declaring that further update might occur.)

The LOCKED Clause

The item-locking statements READU, READVU, and MATREADU are each equipped with an optional LOCKED clause. Normally, when a program attempts to read and lock an item which is already locked, the program waits for the item to be released before continuing with execution. However, if the LOCKED clause is included, the program simply executes the LOCKED statements and continues with execution immediately. The LOCKED statements follow the syntax of THEN and ELSE clauses in mvBASIC.

The LOCKED clause helps to avoid the situation called a deadly embrace. A deadly embrace happens when two users both lock items, and before releasing their locks, each user then tries to read and lock the other item. Without the LOCKED clause, both users will be indefinitely stuck since neither is free to unlock its item. If the LOCKED clause is used, however, the deadly embrace cannot occur.

Select-lists (SELECT, READNEXT)

Select-list variables can be created through the mvBASIC SELECT statement, or by using the EXECUTE statement to call one of the INFO/ACCESS select-list generators. The SELECT statement does not accept the selection expressions accepted by the INFO/ACCESS commands; however, the SELECT statement does allow a select-list to be created from the attributes of a dynamic array string. See External Program Control for more information on using EXECUTE for generating select-lists.

A select-list can also be created external to the program by executing one of the INFO/ACCESS select-list generators and then immediately running the program. If the program is designed this way, the SYSTEM(11) function is recommended to test if there is an external select-list.

Once the select-list is created, it can be read with the READNEXT statement. READNEXT reads the next item- ID in the select-list. After selecting a file, the READNEXT statement is generally used in a loop to perform a procedure on all selected items.

Deleting File Items (DELETE, CLEARFILE)

The DELETE statement is a statement that deletes a specific file item from an opened file. It should not be confused with the DELETE function or the DEL statement, which both delete a field from a dynamic array.

The CLEARFILE statement deletes all items in the data file.

See Also

Overview of mvBASIC Statements and Functions

Assignment Statements

Intrinsic Functions

Internal Program Control

External Program Control

Sending Output to the Screen and Printer

Terminal Input

Dynamic Array Processing

Generalized String Processing

Dimensioned Arrays

Reading and Writing Tapes or Floppy Disks

Communications

Execution Locks

Compiler Directives

Miscellaneous Statements and Functions

The Error Message Processor