Reading Flat File in RPGLE

Reading Flat File in RPGLE
Reading flat file in RPGLE,Reading flat file in AS400,Reading flat file in IBMi.flat file,flat file in AS400, flat file in rpgle, flat file in RPGLE
Reading Flat file in RPGLE

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

Insert data in a Flat file

Let's insert some records manually in a flat file named FLATFILE1.

Insert records in flat file, flat file in AS400,
Records in Flat File

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 perform a Read operation on a Flat file.

Reading record from a flat file in the RPGLE program

The below code will define and read data from a flat-file into a variable defined in RPGLE program. You can create the program by either taking option 14 on the source member on PDM or using the CRTSQLRPGI command for the SQLRPGLE source member attribute.

Let me explain the code which is written above for reading a flat file in the RPGLE program.

RPG Code in Fixed format for reading a Flat file in AS400
*Header Specification                                             
HDebug(*Yes)                                                       
HOption(*NoDebugio)                                                
FFlatFile1 IF   E             DISK    RENAME(FLATFILE1:FLATFILE1@) 
F                                     PREFIX(t)                    
 * program variables                                               
D LocalVariable   S            100A                                
                                                                   
C     *START        SETLL     FLATFILE1                            
C                   READ      FLATFILE1                            
C                   DOW       Not%EOF(FlatFile1)                   
C                   EVAL      LocalVariable = tFlatFile1           
C                   READ      FLATFILE1                            
C                   ENDDO                                          
C                   EVAL      *INLR = *ON
RPGLE Code in /Free and /End-Free format for reading a Flat file in AS400
*Header Specification                                      
HDebug(*Yes)                                                
HOption(*NoDebugio)                                         
FFlatFile1 IF   E             DISK    RENAME(FLATFILE1:FLATFILE1@)
F                                     PREFIX(t)             
 * program variables                                        
 *                                                          
D LocalVariable   S            100A                         
 /Free
  Setll *Start FlatFile1;    
  Read FlatFile1;            
  Dow Not%EOF(FlatFile1);       
    LocalVariable =tFlatFile1;  
  Read FlatFile1;            
  Enddo;                        
  *Inlr = *On;                  
 /End-Free
RPGLE Code in Fully free format for reading a Flat file in AS400
**FREE                                                                      
DCL-F FLATFILE1 DISK USAGE(*INPUT)  PREFIX(T) RENAME(FLATFILE1:FLATFILE1@); 
DCL-S LOCALVARIABLE CHAR(100);                                              
                                                                            
Setll *Start FlatFile1;                                                     
Read FlatFile1;                                                             
Dow Not%EOF(FlatFile1);                                                     
  LocalVariable =tFlatFile1;                                                
  Read FlatFile1;                                                           
Enddo;                                                                      
*Inlr = *On;

 The above code defines a flat-file named FLATFILE1 in RPG in Input fully procedural 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. 

At first set the pointer on the start RRN value on the flat file and then read the record of a flat-file from start till the end of the file in a loop and evaluate the prefixed flat file field to the program variable in RPG and finally set 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
Writing in 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