WRITEBLK data ON filevar [THEN statements] [ELSE statements] WRITEBLK data TO filevar [THEN statements] [ELSE statements]
|data||Data to write to the sequential file. Can be an expression or variable that resolves to a literal value.|
|filevar||A file variable name used to refer to the file in Caché MVBasic. This filevar is obtained from OPENSEQ. The ON and TO keywords are equivalent.|
The WRITEBLK statement is used to write data to a file that has been opened for sequential access using OPENSEQ. You supply this data using the data variable. The data is written as a variable-length “block” (meaning that the data receives no special processing and no special characters are appended). The length of the block is determined by the length of the specified data; the data can be of any length. It has no necessary relationship to logical data units, such as lines or records.
When invoked, WRITEBLK increments a pointer to the end of the data just written, so that repeated invocations of WRITEBLK write sequential blocks of data to the file. The same file pointer is used by WRITEBLK and READBLK.
You can determine the current position of this pointer using the STATUS statement. You can reposition this pointer using the SEEK statement.
To write an end-of-file, use the WEOFSEQ statement.
You can optionally specify a THEN clause, an ELSE clause, or both a THEN and an ELSE clause. If the file write is successful, the THEN clause is executed. If file write fails, the ELSE clause is executed. The statements argument can be the NULL keyword, a single statement, or a block of statements terminated by the END keyword. A block of statements has specific line break requirements: each statement must be on its own line and cannot follow a THEN, ELSE, or END keyword on that line.
You can use the STATUS function to determine the status of the write operation, as follows: 0=sequential write successful; -1=write failed because file not open (or opened by another process).
WRITEBLK and WRITESEQ
The WRITEBLK command writes a string of data to a sequential file. This string may have no relationship to a record within the file. The WRITESEQ command writes a single line of data (a data record) to a sequential file, ending the write by appending two newline characters (carriage return & linefeed) to the data.
Issuing a WRITESEQ creates a new file, if the file specified in OPENSEQ does not exist. Issuing a WRITEBLK does not create a new file. You must issue a CREATE statement to create a sequential file before invoking WRITEBLK.
The following example writes a block of data to an existing sequential file on a Windows system:
OPENSEQ "C:\myfiles\test1" TO mytest IF STATUS()=0 THEN WRITEBLK "John Doe" TO mytest WEOFSEQ mytest CLOSESEQ mytest END ELSE PRINT "File open failed" END
The following example creates a new sequential file and writes a block of data to it. The CREATE statement is mandatory with WRITEBLK:
OPENSEQ "C:\myfiles\test1" TO mytest CREATE mytest WRITEBLK "John Doe" TO mytest WEOFSEQ mytest CLOSESEQ mytest