Opening a File at a Given Position 

To open a file at a particular position, use the AT POSITION addition in the OPEN DATASET statement.

Syntax

OPEN DATASET <dsn> [FOR ....] [IN ... MODE] AT POSITION <pos>.

This statement opens the file <dsn>, and prepares it for reading or writing from position <pos>. <pos> is the number of bytes from the beginning of the file. It is not possible to specify a position before the beginning of the file.

It makes the most sense to specify a position when you are working in binary mode, since the physical representation of a text file depends on the operating system.

DATA FNAME(60) VALUE 'myfile'.

DATA NUM TYPE I.

OPEN DATASET FNAME FOR OUTPUT AT POSITION 16.
DO 5 TIMES.
  NUM = NUM + 1.
  TRANSFER NUM TO FNAME.
ENDDO.

OPEN DATASET FNAME FOR INPUT.
DO 9 TIMES.
  READ DATASET FNAME INTO NUM.
  WRITE / NUM.
ENDDO.

OPEN DATASET FNAME FOR INPUT AT POSITION 28.
SKIP.
DO 2 TIMES.
  READ DATASET FNAME INTO NUM.
  WRITE / NUM.
ENDDO.

The output appears as follows:

0
0
0
0
1
2
3
4
5

4
5

This example opens the file "myfile" in binary mode (this is the default mode). It sets the starting position to 16 for writing to the file, and 28 for reading from it. The example also shows that it only makes sense to specify positions for reading or writing integers if the position is divisible by four.