How can we help you?

We have hundreds of highly-qualified, experienced experts working in 70+ technologies.

IBM i e-Book

A Developer’s Guide to Mastering IBM i Concepts

Control Language (CL)

File Operations

CL, unlike RPG where all types of files are supported, does not support all types of files. Two types of files are supported in the CL procedure or program:

  1. Databases files
  2. Display files

We can send a display to a workstation and receive input from the workstation for use in the CL procedure or program, or we can read data from a database file for use in the CL procedure or program.

There are few important points related to variables used in CL:

1. Datatypes used for CL variables are *CHAR,*DEC,*LGL,*INT,*UINT.
2. Variable names start with ‘&’, for example. &IN03, &Count etc.
3. DCL command is used to declare the variables.
4. Variables from display file will automatically be available to program.
5. CHGVAR command is used to assign values to the variables, for example, CHGVAR VAR(&Count) Value (2).

There are some limitations in CL compared to RPGLE:

CL RPGLE
1.It cannot be used to ADD or UPDATE database files, as it does not have WRITE or UPDATE opcodes like RPGLE. However, we can use RUNSQL command to perform these operations. 1. It can be used to ADD or Update Database files.
2. It does not support subfiles. But it does support one output message subfile. 2. It support all types of subfiles.
3.It does not support program described files. 3. It does support program described files.
4. It does not support printer files. 4. It does support printer files.
5. It does not support Indicator data structure. 5. It does support indicator data structure.
6. It can have only five files(Database and display files) per program. 5. It can have maximum 50 files (Including 8 printer files).

There are few important points related to usage of database files and display files in CL programs:

Database File Display File
1. Only database files with a single record format may be used by a CL procedure or program. 1. Display files may have up to 99 record formats.
2. The files may be either physical or logical files, and a logical file may be defined over multiple physical file members. 2. The file defined must be display file.
3. Only input operations, with the RCVF command, are allowed. 3. All data manipulation commands (SNDF, SNDRCVF, RCVF, ENDRCV and WAIT) are allowed for display files.
4. DDS is not required to create a physical file which is referred to in a CL procedure or program. If DDS is not used to create a physical file, the file has a record format with the same name as the file, and there is one field in the record format with the same name as the file, and with the same length as the record length of the file (RCDLEN parameter of the CRTPF command). 4. The display file must be defined with the DDS.
5. The file need not have a member when it is created for the module or program. It must, however, have a member when the file is processed by the program. 5.The display file must have a member when it is created for the module or program.
6.The file is opened for input only when the first Receive File (RCVF) command is processed. The file must exist and have a member at that time. 6. The display file is opened for both input and output when the first SNDF, SNDRCVF, or RCVF command is processed.
7. The file remains open until the procedure or original program model (OPM) program returns or when the end of file is reached. When the end of file is reached, message CPF0864 is sent to the CL procedure or program, and additional operations are not allowed for the file. The procedure or program should monitor this message and take appropriate action when end of file is reached. 7. The file remains open until the procedure or OPM program returns.

In this, we will describe all the operations which we can perform on databases file. The operations include read, write, update, chain, set lower limit, these operations are equivalent to the RPG files operation and here we will see how we can implement these in CL programs.

We will also describe all the operations which we can perform on display files. We have also listed all the commands which we can use to handle the files in CL programs.

This will also include all the operations or commands related to file operation which are not supported by the CL program or procedure.

Usage:

The operations which we can perform on files:

Databases files:

In CL only input operation can be done on database files using RCVF command. The other operations like write or update on database files cannot be done in CL.

But in the below examples we will see how we can use alternative ways to perform operations like write, set lower limit, chain , update which we are doing in RPG same we can do in CL.

1)Read Operation.

To be able to read a file in a CL program,

  1. First, we must Declare the File using declare file command, DCLF. The file must exist before compilation of the CL program.
  2. Then use the Receive File command, RCVF to read or retrieve data from the file.
1.1) If we want to read the file in loop then, we can use DO loop:
  • We have declared the file STUMASTER which we want to read.
  • We are using Do loop to read the file, so it will read the file from the start.
  • The MONMSG Message ID CPF0864 will tell the end of file is reached and then EXEC command will execute; and it will leave the loop.
1.2) Using Labels:

We have seen above that we have used Do loop for reading the file in loop. Alternative to loops, in CL we can use labels. We use GOTO command to move to the labels.

The below example shows how we can use labels for reading a file till the end of file is reached.

  • READ and END are the labels.
  • On line 4.01, it will read the record from file.
  • On line 4.02, if we reach the end of file then Exec command execute, and it will send the program to the label END.
  • On line 4.03, if we don’t reach the end of file then it will send the program to label READ to execute the RCVF command again.
1.3)  If we want to read more than one file:

We need to use the Open File Identifier, OPNID, parameter to give each file its own unique id. We just use a letter, but the OPNID can be up to ten characters.

  1. When we use the RCVF we need to mention which file to use. The OPNID is used and must match the value in a file declaration.
  2. When we use the OPNID the fields’ name are automatically prefixed with the open identifier and an underscore (_). This ensures that the field names are unique.

We use PREFIX keyword in RPG, for that operation in CL we can use OPNID which will make each field names unique.

1.4) If we want to read a file from a particular record not from start:

This is like what we are doing in RPGLE like SETLL, CHAIN but in CL we don’t have these commands.

But for this we can use Override Database File command, OVRDBF.

In the below example we will see how we can do this,

  • First, we have declared a database file.
  • We assigned the value to variable &key on which we want to position the pointer.
  • Then, we have used the OVRDBF command for overriding the declared file (STUMASTER) using the Starting Position in File parameter; POSITION will position the file pointer to that point in the file when we perform our first “read”. The four parts of this parameter are:
  1. Retrieve order – *KEY this means position the file pointer to the exact match on the key.  The other options we can use are *KEYB, *KEYBE, *KEYA, *KEYAE, *RRN.
  2. Number of key fields – The file which we are reading that file has how many key fields. This file has two key fields.
  3. Record format with the key – by using *N we are telling the command to use the only member in the file.
  4. Key value – this can either be a variable, as we have shown, or we can enter a literal instead.

After positing the pointer at a specific record, we are reading file in a loop.

Note: OVRDBF is used to override the attribute of a physical file. This can make our program to use some other file for the one named to be used in the program.

All overrides are temporary and are effective until where the override command has been in scope.

The parameters of OVRDBF used in below example are;

  • File: specify the file being declared in the program.
  • Position: It tells the starting position for reading records from the database file. Possible values are *NONE, *START, *END, *RRN (provide relative record number i.e. nth record in file), record specified on key field value (*KEY, *KEYA, *KEYAE, *KEYB, *KEYBE).
  • Ovrscope: It tells the scope of the override. There can be three possible values it can have:
  • *ACTGRPDFN: The scope of the override is determined by the activation group of the program that calls this command. when default activation group then scope would be call level of calling program otherwise activation group of the calling program.
  • *CALLLVL: The scope of the override is determined by the current call level. All open operations done at higher or same call level than the current call level are affected by this override.
  • *JOB:The scope of the override is the job in which the override occurs.
2)Write Operation

In CL there is no write command. But we can use Run SQL Command to insert a record into a file.

In the below example we have used SQL query and run it using run SQL command to insert the data into file. In the below example we want to insert values in two fields so for that we have declared two variables &VAR1 and &VAR2 with values. These variables are used in SQL statement to insert value into table.

3) Update Operation:

CL does not have an Update command, so in CL we cannot update database files.

But for this, below we have shown an alternative way to update database files and for that we can use RUNSQL command.

Let’s discuss what we are doing:

  • First, we have declared a file which we want to update.
  • We have assigned the value in the variable (&Key), this we will use to update the record which matches this value.
  • We are positioning the pointer to the &key value in the database file and for this we are overriding the declared file (STUMASTER) using the Starting Position In File parameter, POSITION, this will position the file pointer to that point in the file where we perform our  “UPDATE”.
  • After positioning at specific record, we can use RUNSQL command to update the record. Inside the RUNSQL command we can put the update SQL query.
4) Error handling for file operation in CL:

When RCVF command is used to read the declared file in CL, and if the file is empty or the end of file is reached it will throw a error if it is not handled.

First, we will see if we do not handle error then what happens.

Below is an example of the error.

  • We are reading the file STUMASTER which is empty.

In the code below we are reading the file which is empty using RCVF command.

In the above example, if the file has some data , even then it will throw error when end of file is reached and we have not handled the end of file condition.

To handle the above errors we use MONMSG.

Now in the code,

  • On line 4.02 we have defined the MONMSG with MSGID CPF0864 which tells the end of file is reached or file is empty and the EXEC command executed and it send the program to Endpgm and program ended normally.

To handle any error that occurred in a program we can define one MONMSG and it will handle at the program level.

Below is the example for that.

  • On Line 2.05 we have defined MONMSG with MSGID CPF0000 which is generic message id and it will handle error at program level means any error occur in program it will handle.
  • On Line 4.03 we have used SNDPGMMSG to send a message that error is occurred.

When we call the above code then, we got the message like below

5) Using Commit & Rollback in CL:

Commit means that the changes made in the current transaction are made permanent and Rollback means cancelling all the transactions made in the current transaction. With the help of these commands we can maintain the consistency in the data of files.

To use commit on a file the file must be journaled.

The commit and rollback is used in a block, which starts with a command STRCMTCTL (Start Commitment Control)  and end by a command ENDCMTCTL ( End Commitment control) . Inside these we will perform the operation on file and use the commit or rollback as per requirement.

Let’s see how we can use commit and rollback in CL by a simple example-

In the below code we will first update the file STUMASTER, then we use commit to permanent the changes then we again update the file STUMASTER, then we use rollback to cancel the changes in the file.

Explanation:
  • On Line 2.04, file STUMASTER is declared.
  • On Line 2.08, STRCMTCTL is used with LCKLVL as *CHG which means any change in the file, and this will start the commitment control block.
  • On Line 2.10, we position the pointer on the file equal to the key value.
  • On line 2.12, we are reading the file using RCVF.
  • On line 2.14, we are updating the value of field Class in file with 5 where Field Name = ‘CNTRL’.
  • On line 2.20, we used Commit which makes the changes in the file permanent.
  • On Line 2.21, we update the Field Class by 7 in file.
  • On line 2.25, we used Rollback which means it will cancel the changes done in file just now.

After ROLLBACK, because of Rollback the update of Field ‘CLASS’ to 7 is cancelled and it will  update by previous value.

On line 2.27 ENDCMTCTL (End Commitment Control) is used which end the commitment control block.

B).Display files:

Now let’s see how we can use display files in CL. For the display files three commands are basically used DCLF, SNDF, SNDRCVF. The SNDRCVF is like what we have in the RPGLE as EXFMT. Let’s see how we can use display files in CL using an example shown below.

The DDS of the display file named AIRTHDSP where we are finding the position of character from the entered string.

Now the program using above display file is:

In the program we can see,

  • First using DCLF command we have declared the display file AIRTHDSP.
  • Then we have used SNDRCVF which is used to receive and send data to display screen, so basically it will show the display screen.
  • Then we have put the logic to perform the operation which we want to perform on display file.

Let’s see the result of the above example.

In the entered string we have to find the position of $ which is 26.

Commands that are used in CL for file handling:

1.DCLF : This command is used to declare a display or database file to your CL procedure or program. The Declare File (DCLF) command cannot be used to declare files such as tape, printer, and mixed files.

The file must exist before the module or program is compiled.

2. The only commands we can use with a display file to send or receive data in CL procedures and programs are the Send File (SNDF)Receive File (RCVF), and Send/Receive File (SNDRCVF) commands.

2.1.SNDF : The Send File (SNDF) command is used by a CL program or ILE CL procedure to send a record to a display device that is being used by an interactive user.

  • This command is valid only within a CL program or ILE CL procedure.
  • This command is valid only for display files.
  • This command cannot be used with database files.

2.2.RCVF : The Receive File (RCVF) command is used by a CL program or ILE CL procedure to receive data from a display device or database file. The command reads a record from the file and puts the data from the record into one or more CL variables. The data that is entered by a user at the display or is contained in the input record is copied into CL variables in the program by the RCVF command, where it is processed by the program.

If the file has not been opened by a previous RCVF, SNDRCVF, or SNDF command, it is opened by this command. If the file has been previously closed due to an end-of-file condition on a previous RCVF command, an error occurs.

  1. SNDRCVF : The Send/Receive File (SNDRCVF) command is used by a CL program or ILE CL procedure to send data to and receive data from a device that is being used interactively by a user. If the device file has not been opened, it is opened by this command.

This command is valid only within a CL program or ILE CL procedure and only for display files. It cannot be used with database files.

Restrictions:

There are some restrictions or don’ts for files in CL, and those are listed below:

1. The WAIT and DEV parameters on the Receive File (RCVF) command are not allowed for database files. In addition, the SNDF, SNDRCVF, and ENDRCV commands are not allowed for database files.
2. The CL does not support Indicator data structure, so in display file DDS INDARA keyword should not be used.
3. We don’t have write or update commands in CL for add or update of record in databases files, for this we have discussed the alternative above.
4. CL does not support subfiles , but a single output message subfile is a special type of subfile that is supported well in CL.
5. CL cannot use Program described files.
6. CL cannot use Printer files.
7. CL can have only five files (display or database file) per program.
Code Example:

In the below example we will cover both database file and display file, what we have learned above.

In the example,

  1. First, we have a database file having four fields Enrollnum, Name, Batch, Department.
  2. What we have to do is first we have to create a display file and from the display file we have to enter the enrollment number for which we want the record, and if enrollment number found in file, then it should show the records related to that enrollment number on the screen.

The below screen shot is the display file DDS.

The below screen shot is the program for the above example.

Let’s discuss the above example code line by line.

  1. From line 2.01 to 2.05, we have declared the variables required in the code.
  2. On line 2.10 and 2.11, we have declared the display file and database file and because here two files are declared so we have used OPNID.
  3. Inside the loop on line 2.15, we have used SNDRCVF to read and write the display screen.
  4. On Line 2.25, we put a condition that if F3 is on then exit from screen.
  5. On Line 2.27 to 2.30, we have put a condition that if we have not entered the enrollment number on screen then it throw error.
  6. On line 2.32, we have assigned the enrollment number to the variable &key which is used to chain this value on database file.
  7. On line 2.39 to 2.40, we have used OVRDBF command to position the pointer on the record of database file which matches with the &key value.
  8. On Line 2.42, we have used RCVF to read the matched record from the database file.
  1. On line 2.43 to 2.46, we have put logic to manage the error.
  2. On line 2.49 to 2.52, we are assigning the values to the display screen fields from the database file fields. One important thing here is that on each database file fields we have prefixed the OPNID value.
  3. On line 2.56, we have used RCLRSC which is used to make sure that every thing is closed and override cleared properly.

Now let’s see the output we will get when we run the above example.

How can we help you?

We have hundreds of highly-qualified, experienced experts working in 70+ technologies.

share_iconShare
X

Awards and Certifications

company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo