Writing in flat file in RPGLE

Writing in flat file in RPGLE

Writing data in flat file in RPGLE, Flat file, flat file in as400, flat file in rpgle, flat file in ibmi, write operation on flat file in as400
Introduction

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.

CRTPF, Create Flat File in AS400, Create File in IBMi
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.

RPG Code in Fixed format for writing data in Flat file in AS400

 *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    
RPGLE Code in /Free and /End-Free format for writing data in Flat file in AS400

 *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     
RPGLE Code in Fully free format for writing data in Flat file in AS400

**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.

Did you find the information listed in this article helpful? If you enjoyed this article, share it with your friends and colleagues!

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

Post a Comment

© AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain