Reads a line of data from a sequential file.
READSEQ data FROM filevar [ON ERROR statements] [THEN statements] [ELSE statements]
|data||Name of a variable used to receive a line of data from a file.|
|FROM filevar||A file variable name used to refer to the file in Caché MVBasic. This filevar is obtained from OPENSEQ.|
The READSEQ statement is used to read a line of data from a file that has been opened for sequential access using OPENSEQ. This line of data is written to the data variable.
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]).
A line of data is defined as a unit of data terminated by a newline character. Newline characters are not returned as part of data. When invoked, READSEQ increments a pointer to the next sequential unit of data, so that repeated invocations of READSEQ read sequentially through the file data. The same file pointer is used by READSEQ and WRITESEQ.
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 an ON ERROR clause, which is executed if the file is located but could not be read. If no ON ERROR clause is present, the ELSE clause is taken for this type of error condition. The statements argument can be the NULL placeholder 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; there must be a line break between the ON ERROR keyword and the first line.
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.
READSEQ and READBLK
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. 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 following example reads the first line of data from an existing sequential file on a Windows system:
OPENSEQ "C:\myfiles\test1" TO mytest IF STATUS()=0 THEN READSEQ mydata FROM mytest IF mydata="" THEN PRINT "no data" END ELSE PRINT "the first line:",mydata END WEOFSEQ mytest CLOSESEQ mytest END ELSE PRINT "File open failed" END