Read Physical File in CL
Q. I have a physical file that I want to read sequentially. When a record is read, I need to perform a function, then read another record. I need to know how to read a file sequentially in CL without using an RPG program. In addition, after I read it, how do I determine if a record contains "AB" in columns 75-76?
A. First, you need to declare the file to the CL program, using the Declare File (DCLF) command. Don't bother with any parameters except FILE.
Then, issue a Receive File (RCVF) command within a loop. After the RCVF, be sure to monitor for CPF0864 (type *ESCAPE), which is sent at end of file. When CPF0864 arrives, exit the loop. The program skeleton in 3 illustrates the process.
Then, issue a Receive File (RCVF) command within a loop. After the RCVF, be sure to monitor for CPF0864 (type *ESCAPE), which is sent at end of file. When CPF0864 arrives, exit the loop. The program skeleton in Figure 3 illustrates the process.
Notice that the RCVF command requires no parameters when processing a physical file since there is only one record format.
When you declare the file with DCLF, the CL compiler creates a separate variable for each field in the record if there's an external definition for the file (DDS). If there isn't, the CL compiler creates a single variable (of type *CHAR) for the entire record.
To determine if a record contains "AB" in columns 75-76, you must follow one of the two methods shown below, depending on whether or not the file is externally defined.
1. With External File Definition: Determine which field contains the "AB" in columns 75 and 76, and manipulate the field in whatever form is necessary to extract these two bytes. For instance, %SST (&field_name 75 2) and compare that against "AB".
2. Without External File Definition: This is the simplest case. When you DCLF FILE(MASTER), the CL compiler creates a character variable named &MASTER (the same name as the file). Then you can do:
IF COND(%SST(&MASTER 75 2) *EQ + 'AB') THEN(...)
TechTalk: Read Physical File in CL
Figure 3 Reading a file in CL
Figure 3: Reading a File in CL PGM DCLF FILE(MASTER) LOOP: RCVF MONMSG MSGID(CPF0864)
EXEC(GOTO CMDLBL(END_LOOP)) : : GOTO CMDLBL(LOOP) END_LOOP:
(program continues here after all records are read)
LATEST COMMENTS
MC Press Online