Sequential File I/O |
![]() ![]() ![]() |
Directory files are so called because they are represented by an operating system directory. The records in these files are represented by operating system files in the directory. These files do not give the high performance of hashed files but they allow access to the data from outside of QM. They are therefore particularly useful for data interchange.
Records in directory files are sometimes very large and may consist of a number of lines of textual information with a fixed layout. In such cases, it may be useful to process the data line by line. QM provides statements to perform sequential reading or writing of text data. These can only be used with directory files.
An item is opened for sequential processing using the OPENSEQ statement. This has two forms, one that opens a record in a directory file by name: OPENSEQ file, id TO filevar the other opens a file by pathname: OPENSEQ pathname TO filevar
In both forms, the statement takes the optional ON ERROR, LOCKED, THEN and ELSE clauses. At least one of the THEN and ELSE clauses must be present. Because the OPENSEQ operation is effectively opening a record, it applies a lock to this record to prevent other users overwriting it.
The OPENSEQ statement will take the ELSE clause for three reasons:
The last of these three situations would be an error in a program that is intending to read the item but is usually not an error in a program that will write to the item. The STATUS() function can be used to determine which of the above three conditions exist as discussed in the detailed OPENSEQ statement description.
OPENSEQ also has options to open the item in read-only mode, append to an existing item, or overwrite an existing item.
The QMBasic statements that can be used to access the sequential item are:
Examples
OPENSEQ 'C:\PRICES' TO SEQ.F ELSE STOP 'Cannot open price data' OPEN 'STOCK' TO STK.F ELSE STOP 'Cannot open STOCK' LOOP READSEQ TEXT FROM SEQ.F ELSE EXIT STK.ID = TEXT[1,5] READU STK.REC FROM STK.F, STK.ID THEN STK.REC<STK.PRICE> = ICONV(TEXT[6,8], 'MD2') WRITE STK.REC TO STK.F, STK.ID END ELSE RELEASE STK.F, STK.ID DISPLAY 'Stock item ' : STK.ID : ' not found' END REPEAT
This short program reads lines from a text file, C:\PRICES. Each line within this file has a stock part number in the first five characters and a new price in external format in the next eight characters. For each line, the program reads the corresponding STOCK file record and updates field STK.PRICE to contain the internal form of the price value. The token STK.PRICE would typically be defined in an include record.
OPENSEQ 'C:\IMPORT.CSV' TO SEQ.F ELSE STOP 'Cannot open import file' OPEN 'STOCK' TO STK.F ELSE STOP 'Cannot open STOCK' LOOP READCSV FROM SEQ.F TO STK.ID, PRICE ELSE EXIT READU STK.REC FROM STK.F, STK.ID THEN STK.REC<STK.PRICE> = ICONV(PRICE, 'MD2') WRITE STK.REC TO STK.F, STK.ID END ELSE RELEASE STK.F, STK.ID DISPLAY 'Stock item ' : STK.ID : ' not found' END REPEAT
This program is a variation on the first example where the import data contains comma separated items as might have been written by a spreadsheet tool such as Excel. The READCSV statement reads the first two comma separated items in each line of text into STK.ID and PRICE. Any additional values on the line are discarded.
OPENSEQ 'C:\EXPORT.CSV' OVERWRITE TO SEQ.F ELSE IF STATUS() THEN STOP 'Cannot open export file' END OPEN 'STOCK' TO STK.F ELSE STOP 'Cannot open STOCK' SELECT STK.F TO 1 LOOP READNEXT STK.ID FROM 1 ELSE EXIT READ STK.REC FROM STK.F, STK.ID THEN WRITECSV STK.ID, OCONV(STK.REC<STK.PRICE>, 'MD2'), STK.REC<STK.QOH> TO SEQ.F ELSE STOP 'Write error' END END REPEAT
This program creates a text item in C:\EXPORT.CSV where each line contains the stock part number, the price and the quantity on hand as a comma separated list suitable for import into spreadsheets such as Excel.
|