Caché MultiValue Basic Reference
READBLK
[Home] [Back] [Next]
InterSystems: The power behind what matters   
Class Reference   
Search:    

Reads a block of data from a sequential file.
Synopsis
READBLK data FROM filevar,blksize [THEN statements] [ELSE statements]
Arguments
data Name of a variable used to receive a block of data from a file.
filevar A file variable name used to refer to the file in Caché MVBasic. This filevar is obtained from OPENSEQ.
blksize A positive integer specifying the block size, in bytes.
Description
The READBLK statement is used to read a block of data of a specified size from a file that has been opened for sequential access using OPENSEQ. This block of data is written to the data variable. The specified blksize can be any size.
The data argument accepts a single dynamic array reference (A<i>), a single substring reference (A[s,l]), or a substring reference nested inside a dynamic array reference (A<i>[s,l]).
When invoked, READBLK increments a pointer to the end of the data just read, so that repeated invocations of READBLK read sequentially through the file data. The same file pointer is used by READBLK and WRITEBLK. If the file contains less data than blksize, the available data is read.
You can determine the current position of this pointer using the STATUS statement. You can reposition this pointer using the SEEK statement.
You can optionally specify a THEN clause, an ELSE clause, or both a THEN and an ELSE clause. If the file read is successful, the THEN clause is executed. If file read fails, or if the end of the file is reached, 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 read operation, as follows: 0=sequential read successful; -1=read failed because file not open (or opened by another process); 1=end-of-file encountered; 2=read timed out.
READBLK and READSEQ
The READBLK command retrieves data from a sequential file in blocks of a specified length. These blocks may be of any length, and have no necessary relationship to the length of logical data units, such as lines or records, within the file. The READSEQ command retrieves a single line of data from a sequential file. A line of data is identified by the presence of end-of-line characters. A line of data may be of any size.
Examples
The following example reads the first 100 bytes of data from an existing sequential file on a Windows system:
OPENSEQ "C:\myfiles\test1" TO mytest
   IF STATUS()=0 
   THEN
     READBLK mydata FROM mytest,100
     IF mydata=""
     THEN PRINT "no data"
     END
     ELSE PRINT mydata
     END
     WEOFSEQ mytest
     CLOSESEQ mytest
   END
   ELSE
     PRINT "File open failed"
   END
See Also