Setting a position in the file

 

After a file is opened by a job, the system maintains a position in the file for that job. The file position is used in processing the file.

For example, if a program does a read operation requesting the next sequential record, the system uses the file position to determine which record to return to the program. The system then sets the file position to the record just read, so that another read operation requesting the next sequential record can return the correct record. The system keeps track of all file positions for each job. In addition, each job can have multiple positions in the same file.

The file position is first set to the position specified in the POSITION parameter on the Override with Database File (OVRDBF) command. If you do not use an OVRDBF command, or if you take the default for the POSITION parameter, the file position is set just before the first record in the member's access path. A program can change the current file position by using the appropriate file positioning operation of a high-level language (for example, SETLL in the RPG language or START in the COBOL language). A program can also change the file position by using the Position Database File (POSDBF) command.

File positioning by means of the OVRDBF command does not occur until the next time the file is opened. Because a file can be opened only once within a control language (CL) program, this command cannot be used within a single CL program to affect what is read through the Receive File (RCVF) command.

At end of file, after the last read, the file member is positioned to *START or *END file position, depending on whether the program was reading forward or backward through the file. The following diagram shows *START and *END file positions. *START is at the top, with three records following, and *END at the bottom.

Example of a file

Only a read operation, force-end-of-data operation, high-level language positioning operation, or specific CL command to change the file position can change the file position. Add, update, and delete operations do not change the file position. After a read operation, the file is positioned to the new record. This record is then returned to your program. After the read operation is completed, the file is positioned at the record just returned to your program. If the member is open for input, a force-end-of-data operation positions the file after the last record in the file (*END) and sends the end-of-file message to your program.

For sequential read operations, the current file position is used to locate the next or previous record on the access path. For read-by-key or read-by-relative-record-number operations, the file position is not used. If POSITION(*NONE) is specified at open time, no starting file position is set. In this case, establish a file position in your program, if you are going to read sequentially.

If end-of-file delay was specified for the file on an OVRDBF command, the file is not positioned to *START or *END when the program reads the last record. The file remains positioned at the last record read. A file with end-of-file delay processing specified is positioned to *START or *END only when a force-end-of-data (FEOD) occurs or a controlled job end occurs.

You can also use the POSDBF command to set or change the current position in your file for files opened using either the Open Database File (OPNDBF) command or the Open Query File (OPNQRYF) command.

 

Parent topic:

Basic database file operations in programs

 

Related concepts


Control language
Waiting for more records when end of file is reached

 

Related reference


Override with Database File (OVRDBF) command