DCL-F in RPGLE - File Handling with Cheesy Examples

What does Declare F mean -- DCL-F?

DCL-F declares files in free-format RPGLE, letting you read, write, or display data like a pro. Think of it as the pizza oven where your data bakes whether it’s a database file, a display screen, or a printed receipt.

Let's look at a code example. This is not meant to be a working program, but a showcase for how the code should work. The syntax is as straightforward as a classic pepperoni:

DCL-F file-name device USAGE(*INPUT | *OUTPUT | *UPDATE | *DELETE) [attributes];

You specify the file, its device type (e.g., disk, workstation, printer), and how you’ll use it (input, output, etc.).

It’s like telling the kitchen whether you’re prepping ingredients, serving customers, or printing bills.

DCL-F Code Breakdown

This program processes a pizza order from a file at CodeSlice Pizzeria, showcasing DCL-F variations.

// Lesson: Simple DCL-F for Reading and Updating in IBM i RPGLE
// Purpose: Show basic file read/update with a pizza-ordering example
// Author: Nick Litten (the RPGLE pizza file wizard)

// Declare constants - the bedrock of our pizzeria
DCL-C PIZZA_PRICE 12.99; // Cost of a large pizza
DCL-C SHOP_NAME 'CodeSlice Pizzeria'; // Our pizza shop’s name

// Declare file with DCL-F - our pizza order file
DCL-F PIZZAORD DISK(*EXT) USAGE(*INPUT:*UPDATE) KEYED; // Read and update

// Declare standalone variables - for order processing
DCL-S OrderMsg CHAR(100); // Message to display order summary
DCL-S TotalCost PACKED(7:2); // Total cost for current order

// File record format fields (assumed for PIZZAORD)
DCL-S OrdId PACKED(6:0) INZ(0); // Order ID (key field)
DCL-S OrdQty PACKED(3:0) INZ(0); // Number of pizzas
DCL-S OrdStatus CHAR(1) INZ(''); // Order status (e.g., 'P' for processed)

// Main procedure - let’s handle some pizza orders!

// Read first order from PIZZAORD by key
OrdId = 1001; // Sample order ID
CHAIN OrdId PIZZAORD; // Read record by key

// Process the order
IF %FOUND(PIZZAORD); // Check if order exists
  // Calculate total cost
  TotalCost = OrdQty * PIZZA_PRICE;

  // Update order status to 'Processed'
  OrdStatus = 'P';
  UPDATE PIZZAORD; // Write updated record

  // Build order summary
  OrderMsg = SHOP_NAME + ': Order ' + %CHAR(OrdId) +
  ', ' + %CHAR(OrdQty) + ' pizzas, Total: $' +
  %CHAR(TotalCost) + ', Status: Processed';

ELSE;

  OrderMsg = SHOP_NAME + ': Order ' + %CHAR(OrdId) + ' not found!';
  // NOTE the record is locked so you could UNLOCK it here
  UNLOCK PIZZAORD;

ENDIF;

// Display the order summary
DSPLY OrderMsg;

// Clean up and get ready for the next order
*INLR = *ON;
RETURN;

Constants

  •  PIZZA_PRICE: $12.99 for a large pizza.
  • SHOP_NAME: Our pizzeria’s name, CodeSlice Pizzeria

File Declaration

PIZZAORD DISK(*EXT) USAGE(*INPUT:*UPDATE) KEYED: A database file for reading and updating pizza orders, accessed by a key (order ID).

Variables

  •  OrderMsg: String for the order summary.
  • TotalCost: Packed decimal for the order’s total cost.
  • OrdId, OrdQty, OrdStatus: Fields for order ID (key), quantity, and status, assumed to match PIZZAORD’s record format.

Logic

  •  Sets a sample OrdId (1001) and uses CHAIN to read the record from PIZZAORD by key.
  • If the order is found (%FOUND), calculates the total cost (OrdQty * PIZZA_PRICE), sets OrdStatus to 'P' (processed), and updates the record with UPDATE.
  • Builds a summary message, or notes if the order wasn’t found.
  • Displays the result with DSPLY, because console output is the coder’s high-five.

DCL-F is like the pizza oven’s order queue: it lets you grab and update orders efficiently.

Using USAGE(*INPUT:*UPDATE) with KEYED access means you can quickly find and modify records, keeping your program as smooth as a well-timed delivery. It’s perfect for managing database files like customer orders.

Name your files clearly, like PIZZAORD instead of FILE1, and ensure field names match the file’s record format. Vague names or mismatches are like delivering a pizza to the wrong address!

    • This code example is just a working example to show how things might work when you are coding… but… yuo’re comment has inspired me to make this into a working example 🙂

      Next week, I will make a file, and this code sample and record a little video on how it all works.

    • This code example is just a working example to show how things might work when you are coding… but… you’re comment has inspired me to make this into a working example 🙂

      Next week, I will make a file, and this code sample and record a little video on how it all works.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >