Writing Data

In PRO/5, writing to a file is as easy as reading from a file. The WRITE command takes the same options and the same form of an argument list. (You may be able to use IOLISTs. See the IOLIST verb for the syntax and examples.) As an example:

>WRITE (1,KEY="AAA")A$,B$,A,B,C

This example would create the key "AAA" (assuming the file opened on channel 1 is a keyed file) and place the variables A$,B$,A,B,C into the record. Each variable would be written to the record with a $0A$ (linefeed) appended. If the number of bytes in all of the variables, plus the linefeeds, exceeded the defined record size, an !ERROR=1 would be generated. Otherwise, PRO/5 would fill to the end of the record with $00$s. (This keeps you from reading old data regardless of the size of succeeding writes.)

If you are writing data that you read using READ RECORD, you should use WRITE RECORD. Otherwise PRO/5 will attempt to place a field separator at the end of the record, generating a record one byte longer than the file's record size.

The DOM= option on a WRITE will be taken if you access a record already defined by a key. This is commonly used when you intend to create a new record and want to guarantee that you will not overwrite an existing record.

As an example of file accessing, consider the following code fragment:

1000 REM - get account
1010 INPUT (0,ERR=1010)@(23,5),ACCTMASK$+"",@(23,5),
1010:ACCT$:(""=9000,"END"=9000,"end"=9000,LEN=1,9)
1020 PRINT (0,ERR=1000)@(23,5),ACCT$:ACCTMASK$
1030 EXTRACT RECORD(1,DOM=1100,KEY=ACCT$) ARMASTER$;
1030:PRINT @(35,5),ARMASTER.NAME$,; GOTO 2000
1100 REM - new account?
1110 INPUT (0,ERR=1110)@(30,20),
1110:"Is this a new account? ",
1110:TEMP$:("Y"=1120,"y"=1120,"N"=1120,"n"=1120)
1120 PRINT @(30,20),'CE',
1130 ON POS(TEMP$="YyNn") GOTO 1100,1140,1140,1000
1140 LET ARMASTER.ACCT$=ACCT$,ARMASTER.BUSY$="Y"
1150 WRITE RECORD (1,DOM=1000)ARMASTER$
1160 EXTRACT RECORD (1,KEY=ARMASTER.ACCT$)ARMASTER$
1170 GOTO 3000

The EXTRACT on line 1030 uses the KEY= to look for a specified record and the DOM= option to detect the presence or absence of the requested master file record. The WRITE on line 1150 creates the new record and uses the DOM= option to detect the (vague) possibility that another user has already started adding the new master file record. Finally, the master file record contains a "busy" flag to handle the instance of another user possibly retrieving the newly created record before the EXTRACT on line 1160.

The data written to a file may be modified on a character by character basis using the TBL= option. The translation of characters is done preceding write and not on any key provided using the KEY= option.