When it comes to file processing, one of the best techniques used in service programs today is the %OPEN built-in function.
For years, we RPG programmers often used an error indicator on the OPEN opcode or a field/switch containing the opened/closed state of a file declared in our programs. With %OPEN, we can avoid all that.
%OPEN allows you to check the opened/closed state of a file declared on your File specifications. If the file is not open, you can open it; if the file is already open, you can avoid opening it again.
To use %OPEN in your Calculation specifications, wrap the name of the file as declared on your File specifications with %OPEN(). The value returned by this built-in function is essentially an on/off indicator: *ON if the file is open and *OFF if the file is closed.
The following is an example of testing for a not-open condition and then opening a file and testing for a successful open.
D Open_Failed C Const(1217)
C if not %OPEN(CustMast)
C Open(e) CustMast
C if %Error()
C if %Status(CustMast) = Open_Failed
** Do something to handle the failed open here.
C endif
C endif
The File specification keyword USROPN (user controlled open) is used in the above example. This keyword is often misunderstood. You use USROPN to cause the RPG cycle to avoid automatically opening the file. That is the only thing it does. You may still use the CLOSE and OPEN opcodes on that file. In fact, often an application will automatically open a file and then close it when it returns (with LR=*OFF). Then, upon reentering the program, it will use %OPEN to test if that file has already been opened or has been closed. It then opens the file as needed.
So remember, USROPN means "allow the RPG cycle to initially open the file when the program is initially called."
Use %OPEN. It's pretty cool.
Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language--the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.
LATEST COMMENTS
MC Press Online