Previous Topic Next topic Print topic


Consecutive Record I/O

A record file without keys is called a consecutive file. The records of such a file can be read only in the same sequential order in which they were written. Thus, a consecutive file can be read only one record after the other, starting with the first record in the file. A record of a consecutive file can be rewritten, but only immediately after it has been read.

A consecutive file is declared by including the CONSECUTIVE option of the ENVIRONMENT attribute. For example,

DECLARE ULOG RECORD OUTPUT FILE 
        ENVIRONMENT(CONSECUTIVE RECSIZE(130));

In this example the RECSIZE option causes all records to be of length 130 bytes. If RECSIZE is omitted, variable length records may be written. In the latter case, there is no information automatically recorded in the file to indicate the length of the records. A program reading the file must have the ability to know how many bytes to read with each READ statement.

Example:

CONFILE: PROCEDURE OPTIONS(MAIN);

   DECLARE TEST RECORD OUTPUT FILE ENV(CONSECUTIVE); 
   DECLARE REC1 CHAR(5) INIT('AAAAA');
   DECLARE REC2 CHAR(10) INIT('BEEBBBBBBB');

   OPEN FILE(TEST) TITLE('testdata'); 
   WRITE FILE(TEST) FROM(REC1);
   WRITE FILE(TEST) FROM(REC2); 
   WRITE FILE(TEST) FROM(REC1); 
   CLOSE FILE(TEST);

END CONFILE;

The file testdata resulting from this program will contain the following 20 bytes:

AAAAABBBBBBBBBBAAAAA
Note:

Open PL/I has an older method of accomplishing the same thing. It is done by using the TITLE option of the OPEN statement rather than the ENVIRONMENT attribute of the DECLARE statement. The following illustrates the old method.

DECLARE RECORD TEST OUTPUT FILE;
OPEN FILE (TEST) TITLE ('testfile -SAM');
Previous Topic Next topic Print topic