# WRITE

Syntax:

Then: WRITE
Then: WRITE filename

WRITE, by itself, without a filename argument, forces all open files be updated to their in-memory versions.

WRITE filename, instructs that just the designated file be updated to its in-memory version.

Version Ref: 4.5 (not included in filePro Lite)

IMPORTANT: On multi-user systems, "WRITE filename" also causes the lock to be removed from "lookup files" which have had a lock placed on them by the p lookup option. In other words, on multi-user systems, it is possible for multiple users to view a record at the same time. However, only one user at a time can UPDATE a record. This is because while the user is updating the record, it is locked by filePro. Other users may view it, but may not write to that record until the locking user unlocks it. This prevents records from getting corrupted by two people changing data on them at the same time. If the record is retrieved by a lookup and the -p flag is used on the lookup, then no one else can update that record until the lock is removed. This is done with the "WRITE filename" command. You should write your code so as to retrieve records, update them, and release them as soon as possible. This keeps your databases open and easily used by everyone.

getnum If:
Then: lookup ctl=control r=r np
If: not ctl
Then: show "@problem, no control record" ; exit
Then: 1=ctl(4) ; ctl(4)=ctl(4)+"1" ; write ctl ; return

This code gets a unique number from a control file. Then it increments the value in the control file so that the next user coming for a unique number will get the next higher number. However, because the -p flag is used on the lookup line, this particular record is "locked" and no other process can retrieve a unique number until it is unlocked. This prevents two or more users from getting the same unique number! That is why there is nothing else between getting the unique number, incrementing the control number by "1", and then running the WRITE command against the control file. The WRITE command not only hands all the fields of the record to the O/S for storage on disk, but it ALSO unlocks the file at this particular record. Anyone else can now obtain the next available unique number, it will be one higher than the last number retrieved, and the next retrieval will increment it for the next user after that and so on. The idea to grasp here is that the locked record is not held in a locked state for any longer than absolutely necessary. Get the unique number, increment the control number, write the file to ensure that the change is written to disk before anyone else can retrieve this record, and obtain the next unique number.

NOTE: The CLOSE filename statement will also perform an "unlock" of a designated file. However, this will also close the file, and the next user accessing this file to obtain anything will have to wait a little longer while the O/S "opens" the file again. In a case where many hits will be made on a file in a short period of time, it is best to simply WRITE the file each time it is updated to provide faster operations.

HINT: If you are working on a new record, the system-maintained fields for that record will be empty until an automatic WRITE happens at the end of processing, or until you issue a WRITE command on the processing table. A good check to see if a record is new is to test whether @cd (creation date) is equal to "" (null). If it is, this record is currently new and has never been written to disk.

Description:

WRITE is a very important command. It has two important functions. The first is to force the memory copies of all the fields in a record to be written out to disk. (Actually they are just handed to the O/S whose job it is to really write them to disk. Another filePro command, SYNC, forces the O/S itself to write all of its buffers to disk. This means things waiting to be written to disk actually get written.) However, for most practical purposes, you can assume that issuing a WRITE is a guarantee that your data will be written to disk. The second function of WRITE is to unlock a specified file or all files if no file is specified.

Technical Notes

WRITE is different from the WRITE( ) function, which writes regular files opened with the OPEN( ) or CREATE( ) functions.