Example of REGIONAL(1) I/O

/* This program creates and updates a REGIONAL(1) file */ 

sample: procedure options(main);
   declare File1 file environment(regional(1) recsize(20));/*requires -defext 
   declare Buffer char(20);
   declare FirstByte char(1) defined(Buffer);
   declare Key char(4) varying;
   declare Eof bit;

   put skip list('Creating REGIONAL(1) file with records 1, 5 and 9.');
   open file(File1) output;
   Buffer = copy('1',size(Buffer));
   write file (File1) from(Buffer) keyfrom (1); 
   Buffer = copy('5',size(Buffer));
   write file (File1) from(Buffer) keyfrom (5); 
   Buffer = copy('9',size(Buffer));
   write file (File1) from(Buffer) keyfrom (9); 
   close file(File1);

   open file(File1) update;
   on endfile(File1) Eof = '1'b1;
   Eof '0'b1;       /* Initialize */
   put skip list('Reading file and updating the even numbered records.');
   read file(File1) into(Buffer) keyto(Key);
   do while (^Eof);
     if unspec(FirstByte) = 'FF'b4 then do;  /* dummy record */
       if (mod(fixed(Key,15),2) = 0) then do;  /* change even number ones *
       Buffer = 'rewritten';
       rewrite file(File1) from (Buffer);  /* rewrite current record */
       end;
     end;
    read file(File1) into (Buffer) keyto(Key);
    end;
   Eof = '0'b1; /* Reset */
   put skip(2) list ('Reading and displaying all records in file.'); 
   Key = '1';
   read file(File1) into(Buffer) key(Key);     /* direct read */
   do while (^Eof);
      if unspec(FirstByte) = 'FF'b4 then put skip list(Key,'<dummy record>'); 
      else put skip list(Key,Buffer);
      read file(File1) into(Buffer) keyto(Key);  /* sequential read */ 
      end;
   close file(File1);
   put skip;
end sample;

You can compile, link, execute, and display the output of the above sample program with the following commands:

mfplx sample.pl1 -defext -o sample.out 
sample.out

The output is as follows:

Creating REGIONAL(1) file with records 1, 5 and 9. 
Reading file and updating the even numbered records.

Reading and displaying all records in file.
1       11111111111111111111
2       rewritten
3       <dummy record>
4       rewritten
5       55555555555555555555
6       rewritten
7       <dummy record>
8       rewritten
9       99999999999999999999