Q(uick)BASIC Statement: UNLOCK
Quick View
UNLOCK
A file I/O statement that releases locks applied to parts of a file
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- LOCK [#]filenumber% [,{record& | [start&] TO end&}]
- UNLOCK [#]filenumber% [,{record& | [start&] TO end&}]
Description/Parameter(s)
filenumber% | The number of an open file. |
record& | For random-access files, the number of a record to lock, relative to the first record in the file. For binary files, the number of a byte to lock, relative to the first byte in the file. |
start& and end& | The numbers of the first and last records or bytes in a range of records or bytes to lock or unlock. |
|
Example
This example runs only in a network environment.
OPEN "TEST.DAT" FOR RANDOM AS #1
FOR i% = 1 TO 10
PUT #1, , i%
NEXT i%
LOCK #1, 2 'Lock record 2.
GET #1, 2, i%
UNLOCK #1, 2 'Unlock record 2.
Syntax
- UNLOCK [#] filenumber [,{record| [start] TO end}]
Description/Parameter(s)
Tip: | LOCK and UNLOCK are only useful in networked environments where several processes may need access to the same file. They only function if you are using DOS version 3.1 or later. |
The UNLOCK statement is used only after a LOCK statement. See the ⮜ LOCK statement details ⮞ for more information.
For binary mode files, the arguments record, start, and end represent the number of a byte relative to the beginning of the file. The first byte in a file is byte 1.
For random access files, these arguments are the number of a record relative to the beginning of the file. The first record is record 1.
Example
See the ⮜ LOCK statement programming examples ⮞, which use the UNLOCK statement.
See also:
Syntax
- UNLOCK [#]filenumber% [,{record& | [start&] TO end&}]
Description/Parameter(s)
filenumber% | The number used to open the file. |
record& | The number of the record or byte to be locked. |
start& | The number of the first record or byte to be locked. |
end& | The number of the last record or byte to be locked. |
- The argument record& can be any number between 1 and 2,147,483,647, inclusive, (equivalent to 2^31 -1). A record can be up to 32,767 bytes in length.
- For binary-mode files, the arguments record&, start&, and end& represent the number of a byte relative to the beginning of the file. The first byte in a file is byte 1.
- For random-access files, record&, start&, and end& are the number of a record relative to the beginning of the file. The first record is record 1!
Important
- Be sure to remove all locks with an UNLOCK statement before closing a file or terminating your program. Failing to remove locks produces unpredictable results.
- The arguments to LOCK and UNLOCK must match exactly.
- Do not use LOCK and UNLOCK on devices or ISAM tables.
Example
This example illustrates the use of the LOCK and UNLOCK statements. A sample data record is created in a random-access file containing a customer record. The program allows you to update the file, locking the file while you use it to prevent access from another terminal.
Note: In order to use this program, you must have DOS version 3.1 or later, and you must run the DOS SHARE.EXE to enable locking operations before entering the BASIC programming environment.
'Define the record.
TYPE AccountRec
Payer AS STRING * 20
Address AS STRING * 20
Place AS STRING * 20
Owe AS SINGLE
END TYPE
DIM CustRec AS AccountRec
'This section creates a sample record to use.
ON ERROR GOTO ErrHandler
OPEN "MONITOR" FOR RANDOM SHARED AS #1 LEN = LEN(CustRec)
CustRec.Payer = "George Washington"
CustRec.Address = "1 Cherry Tree Lane"
CustRec.Place = "Mt. Vernon, VA"
CustRec.Owe = 12!
PUT #1, 1, CustRec 'Put one record in the file.
CLOSE #1
'This sections opens the sample record for updating.
OPEN "MONITOR" FOR RANDOM SHARED AS #1 LEN = LEN(CustRec)
DO
Number% = 0 'Reset to zero.
DO UNTIL Number% = 1 'Force user to input 1.
CLS : LOCATE 10, 10
INPUT "Customer Number? #"; Number%
LOOP
'Lock the current record so another process
'doesn't change it while you're using it.
LOCK #1, Number%
GET #1, Number%, CustRec
LOCATE 11, 10: PRINT "Customer: "; CustRec.Payer
LOCATE 12, 10: PRINT "Address: "; CustRec.Address
LOCATE 13, 10: PRINT "Currently owes: $"; CustRec.Owe
LOCATE 15, 10: INPUT "Change (+ or -)", Change!
CustRec.Owe = CustRec.Owe + Change!
PUT #1, Number%, CustRec
'Unlock the record so others can use it.
UNLOCK #1, Number%
LOCATE 17, 10: INPUT "Update another? ", Continue$
Update$ = UCASE$(LEFT$(Continue$, 1))
LOOP WHILE Update$ = "Y"
CLOSE #1
'Remove file from disk.
KILL "MONITOR"
END
ErrHandler:
IF ERR = 70 THEN 'Permission denied error.
CLS
PRINT "You must run SHARE.EXE before running this example."
PRINT "Exit the programming environment, run SHARE.EXE, and"
PRINT "reenter the programming environment to run this"
PRINT "example. Do not shell to DOS to run SHARE.EXE or you"
PRINT "may not be able to run other programs until you reboot."
END IF
END
See also: