LOCK statement

Purpose

Lock part or all of an open file, to prevent other processes from accessing it.

Syntax

LOCK [#] filenum& [, {record&& | start&& TO end&&}]

Remarks

LOCK prevents another process from accessing a record, range of records, byte, or range of bytes in a file opened as file number filenum&.

If the file was opened in random-access mode, record&&, start&&, and end&& specify record numbers.  When used with binary mode files, record&&, start&&, and end&& specify byte positions, starting from either zero or one (the default).

If a record is specified, only that record (or byte) is locked.  Otherwise, a range of records (or bytes) is locked, from start&& to end&&.

If no records are specified, or if the file was opened in sequential mode, the entire file is locked.

All records (or bytes) to be locked must be subsequently unlocked using the UNLOCK statement.  Multiple locks may be placed on a file, and locks may be unlocked in any order.  However, the parameters used for each UNLOCK statement must exactly match those used for the previous corresponding LOCK statement.

All locked records (or bytes) must be unlocked using the UNLOCK statement before the file can be closed.

If a lock attempt fails, PowerBASIC sets the ERR system variable to reflect a run-time Error 70 ("Permission denied"), or Error 75 ("Path/file access error").

See also

OPEN, UNLOCK

Example

OPEN "PATIENTS.DAT" FOR RANDOM AS #1 LEN = 1024

' determine the record number to retrieve

LOCK #1, recnum

GET #1, recnum

' process the record here

PUT #1, recnum

UNLOCK #1, recnum

CLOSE #1