At this time I will discuss about reading each record in a file in CLP using the record format in a file. To find out which file format to read, we can use the Display File Field Description (DSPFFD) command.
In CLP, to read a file using the DCLF command first. Automatically all fields in the file become variables in the program that we make. The DSPFFD command is used to find out the record format and fields in the file.
Here is an example:
PGM DCLF FILE(SOOLDAT/TESTFILE) DCL VAR(&OUTPUT) TYPE(*CHAR) LEN(50) LOOP: RCVF RCDFMT(TESTFILE) MONMSG MSGID(CPF0864) EXEC(GOTO CMDLBL(EOP)) CHGVAR VAR(&OUTPUT) VALUE('Name :' *CAT &NAME *TCAT + ', Address : ' *CAT &ADDRESS) SNDPGMMSG MSG(&OUTPUT) GOTO CMDLBL(LOOP) EOP: ENDPGM
Some notes in the program above:
- The file must be declared at the beginning of the program.
- All fields in the file that are declared will automatically become variables in the program (without having to declare variables).
- In reading files, RCVF commands are needed with a record format in accordance with the declared file.
- The concept of reading files is actually like looping each record, so there is a process back to the loop label.