%OPEN Built-In Functions in rpgle
%OPEN function is used to check to see if a file has already been opened.
It returns *ON if the file is opened, otherwise it returns *OFF.
In case we are using USROPN keyword in F-Spec, then during the program initialization the file will be closed and we need to open it explicitly.
If we are not using the keyword USROPN then by default the file will be open during program initialization.
Example –%open built-in function in rpgle
FFILE1 IF E K DISK USROPN
c LoadSub BegSr
c If Not %Open(FILE1)
c Open FILE1
c Endif
c Eval Idx=*Zero
c DoU %Eof(FILE1)
c Read FILE1
c If Not %Eof(FILE1)
c Eval Idx=Idx+1
c Eval CRCD(Idx)=Field1
c Eval CRDS(Idx)=Field2
c EndIf
c EndDo
c Close FILE1
c EndSr
Output
In above example, we are checking if the file is Open and Not using function %Open().
If we find that the file is not Open, we Open it and then process it in the program.