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

Deep Dive into DDS and DDL

Externally Described Files

An externally described file in IBM i (Formerly AS/400 or iSeries) refers to a file that has its structure or format defined outside the program that uses the file and it is described to the OS/400 system at the field level. In an externally described file, the compiler retrieves the description of the fields from an external file-description which was created using DDS or SQL commands. Therefore, you do not have to code the field
descriptions on input and/or output specifications within the RPG source member.
The description includes information about where the data comes from (database or device), and field description and their attributes. The file must exist and be accessible from the library list before you compile the program.

Externally described files offer the following advantages:

  • Code efficiency in RPG/400 programs. If the same file is used by many programs, the fields can be defined once to the OS/400 system and used by all the programs. This practice eliminates the need to code input and output specifications for RPG/400 programs that use externally described files.
  • Requires less maintenance when the file’s record format is changed. You can often update programs by changing the file’s record format and then recompiling the programs that use the files without changing any coding in the program.
  • Improved documentation because programs using the same files use consistent record-format and field names
  • Improved reliability. If level checking is specified, the RPG program will notify the user if there are changes in the external description.

Defining Externally Described Files

When defining an externally described file in fix format you have to provide file format as E whereas in free format you can use DISK(*EXT).

The information in this external description includes:

  • File information like file type, attributes, access method (by key or relative record number)
  • Record-format description, which includes the record format name and field descriptions (names, locations, and attributes).

Types of files that can be described externally in a RPGLE program

  • Physical files (PF): A Physical file or PF is a data file in IBMi which contains actual data that is stored on the system and its description.
  • Logical files (LF): A logical file contains a description of records of a Physical File. They do not contain actual data. It is a view or representation of one or more PFs. A LF cannot exists without a PF.
  • Display files (DSPF): Display files used in IBMi to define the layout of the screens for interactive programs. It allows developers to design user interfaces, specifying screen formats, Input fields and output areas.
  • Printer file (PRTF): Printer file is used to define the format of the output intended for printing or to show reports to user. PRTF specify the arrangement of text and data on printed documents and ensures proper format, presentation, and alignment.

Flat Files

A flat file is a special type of physical file that do not have any hierarchical structure or multiple records formats. It consists of a single field with long length (RCDLEN) which is defined at the time of creation. The maximum length of RCDLEN can be 32766 bytes.

Flat files have no field definitions and no indexes can be built off of them. In a flat file the file name, record format & field name are same.

We can write, read, update, delete the Flat file. Reading and deleting can be done normally whereas update and write operations required the use of Data Structures.

Usage
  • Flat files are mostly used as Output file to copy the data to Stream file on IFS.
  • Flat files are used to store data for Pre-Runtime Arrays.
Creating a flat file.
  • Creating a flat file is same as creating Physical File.
  • It is very easy to declare either in command line or even in DDS in STRSEU editor.

CRTPF (Library/FlatFileName) RCDLEN(50)

Figure 1 : Creating A Flat file

Figure 2 : Creating A Flat file

When we do DSPFFD (Display File Field Description) on a flat file. We can see that the file
name, record format name & field name is same for flat file. The record length provided at the time of creating flat file becomes its field length

Figure 3 : DSPFFD On Flat files

Operations on a flat file:

Reading a Flat File Using RPGLE PGM

Figure 4 : Reading A Flat file

  • Line 2 & 3 : Included a flat file “FLATFILE” in program by declaring it with usage as input and PREFIX W_ to make field name as w_flatfile to the field and renamed the record Format from FLATFILE to Ffile with RENAME.(Note: We need to RENAME the record format to ignore compile-time severity 40 error
    *RNF2109 And we need to add PREFIX to the field, otherwise, we will again get compile-time severity 30 error *RNF7503)

  • Line 4 and 5: are comment.
  • Line 6: We have defined a variable FFvar with the same length as flat file RCDLEN.
  • Line 9: We have set the pointer on the start RRN value on the flat file.
  • Line 10: Read the record of a flat-file from start.
  • Line 11 to 15: we have started a do-while loop till end of file is reached.
  • Line 12: The records from flat file fields is assigned to the variable FFVar.
  • Line 13: Displaying the data in FFvar.
  • Line 14: Read the next data from flat file.
  • Line 17: Set the last record indicator = *ON.
Writing data in a Flat File Using RPGLE PGM

Figure 5: Writing data into a flat file

  • Line 2 & 3 : Included a flat file “FLATFILE” in program by declaring it with usage as input, output, update & delete and PREFIX W_ to make field name as w_flatfile to the field and renamed the record Format from FLATFILE to Ffile with RENAME.
  • Line 6 : Declare a variable FFvar with same length as RCDLEN.
  • Line 12 : Initialize the FFvar with the data.
  • Line 13: Assigned the value of FFVar to the flat file field w_Flatfile.
  • Line 14: Write the data on record format FFile using WRITE Opcode.
  • Line 17: Set the last record indicator = *ON
Chain and Update on Flat File Using RPGLE PGM

Figure 6 : Chain and Update on a Flat file using RPGLE Program

Figure 7 : Data in Flat file before chain and Update

Figure 8 :Data in Flat file after chain and Update

  • Line 2 & 3 : Included a flat file “FLATFILE” in program by declaring it with usage as
    input, output, update & delete and PREFIX W_ to make field name as w_flatfile to the field and renamed the record Format from FLATFILE to Ffile with RENAME.
  • Line 5 : Declare a variable UpdVar with same length as RCDLEN.
  • Line 8 : Initialize the UpdVar with the data.
  • Line 9: Set the data pointer to 1 RRN with Chain operation on flat file.
  • Line 10 – 13: this is an If-Elseif block which get executed when the chain operation has found data in flat
    file.
  • Line 11 : Assigns the value in UpdVar to w_Flatfile field in flat file.
  • Line 12: Update the flat file using UPDATE opcode and record format Ffile.
  • Refer Figure 7 and Figure 8 for data in Flat file before and after Chain-Update operation.
  • Line 15: Set the last record indicator = *ON
Chain and Delete on Flat File Using RPGLE PGM

Figure 9: Chain-Delete on a flat file using RPGLE program

  • Line 2 & 3 : Included a flat file “FLATFILE” in program by declaring it with usage as input, output, update & delete and PREFIX W_ to make field name as
    w_flatfile to the field and renamed the record Format from FLATFILE to Ffile with RENAME.
  • Line 5 : Declare a variable DelVar with same length as RCDLEN.
  • Line 8 : Initialize the DelVar with the data.
  • Line 9: Set the data pointer to 1 RRN with Chain operation on flat file.
  • Line 10 – 13: this is an If-Elseif block which get executed when the chain operation has found data in flat
    file.
  • Line 11 : Assigns the value in DelVar to w_Flatfile field in flat file.
  • Line 12: Delete the flat file record using DELETE opcode and record format Ffile.
  • Refer Figure 10 and Figure 11 for data in Flat file before and after Chain-Delete operation.
  • Line 15: Set the last record indicator = *ON

Figure 10: Data in flat file before chain delete

Figure 11: Data in flat file after chain-delete

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