A flat file is a file without DDS but with record length. A flat file has the same name record format and field name with a field size as the record length.
Create command
A flat file can be created using the CRTPF command as follows. Let's create a flat file named FLATFILE1 in the current library.
FLATFILE1 |
We can perform the READ, WRITE, UPDATE, DELETE operation on the flat file using I/O in RPGLE. So In this blog, we will discuss the example programs to do write operations on the flat file.
Writing record to a flat file in the RPGLE program
The below code will define and write data to a flat file from a variable in the RPGLE program. You can create the program either by taking option 14 on the source member on PDM or using the CRTSQLRPGI command for the SQLRPGLE source member.
Let me explain the code which is written above for writing records to a flat file in the RPGLE program.
*Header Specification
HDebug(*Yes)
HOption(*NoDebugio)
FFlatFile1 O E DISK RENAME(FLATFILE1:FLATFILE1@)
F PREFIX(t)
* program variables
D LocalVariable S 100A
C EVAL LocalVariable = 'TEST DATA'
C EVAL tFlatFile1 = LocalVariable
C WRITE FLATFILE1@
C EVAL *INLR = *ON
*Header Specification
HDebug(*Yes)
HOption(*NoDebugio)
FFlatFile1 O E DISK RENAME(FLATFILE1:FLATFILE1@)
F PREFIX(t)
* program variables
*
D LocalVariable S 100A
/Free
LocalVariable = 'TEST DATA';
tFlatFile1 = LocalVariable;
Write FlatFile1@;
*Inlr = *On;
/End-Free
**FREE
DCL-F FLATFILE1 USAGE(*OUTPUT) PREFIX(T) RENAME(FLATFILE1:FLATFILE1@);
DCL-S LOCALVARIABLE CHAR(100);
LocalVariable = 'TEST DATA';
tFlatFile1 = LocalVariable;
Write FlatFile1@;
*Inlr = *On;
The below code defines a flat-file named FLATFILE1 in RPG in Output mode and it's an external disk file. RENAME is used to rename the record format of the file from FLATFILE1 to FLATFILE1@ and prefix the field with t so that field becomes tFLATFILE1.
Please note that we need to RENAME the record format to ignore compile-time severity 40 error *RNF2109 (All record formats for externally-described files ignored or dropped due to error.
Also, we need to PREFIX the field, otherwise, we will again get compile-time severity 30 error *RNF7503 (Expression contains an operand that is not defined)
Declare a variable of length 100 since the field length of a flat file is 100.
Evaluate the LocalVariable value to the file field tFALATFILE1 in the RPG program and then write record format FLATFILE1@. Finally setting the last record indicator to *ON.
Thanks!
Related Post
Flat File in AS400
Reading Flat File in RPGLE
Chain and Update in Flat file in RPGLE
Chain and Delete in Flat File in RPGLE