Steps to process flat files on AS400 system in RPG IV
Flat files for all usages in RPG IV, are physical files only!
Yes, good news is that flat files are nothing but any other ordinary physical file on AS400 system. If you understand this, you can now process any flat file. Isn’t it?
Actually flat files are nothing but a physical file with only one column. The length of this column is usually larger than a typical column of any other physical file. This is because each record of a flat file actually contains several columns of an ordinary physical file.
Once you understand what actually is a flat file on AS400 system, all you need to worry about is to how to extract different business attributes.
Lets us see what alternative we have?
- Use substring functions to extract specific portion of data.
- Define data structure of exact length as the record length of the flat file. Define as many sub-fields of this data structure as there are business attribute concatenated in the flat file.
When to use what?
In AS400 system you will always find flat files to have specific column widths, So, data structures solution will always be your favorite.
Though I have personally not seen any “delimited flat file” (You understand what I mean by delimited. Don’t you?), Should this be the case some day, you would use the first option (i.e. using substring function) to extract different business attributes.
Steps to process a flat file in RPG IV on AS400 system.
Exact step to process a flat file depends on how the flat file is created.
Two methods to create flat files on AS400 system.
- Method 1. – Create a physical file using CRTPF command. Give the total length of each record of the flat file in record length field. In this situation, system creates a physical file. The object name, the record format name and field name of this physical file are the same as you specify while creating the physical file.
If you created your physical files using this method, you need to rename your record format and field names inside RPG IV program.
After this, you define a data structure. Each subfield of this data structure represents each business attribute. Total of lengths of attributes equals to the record length of the flat file.
Lastly you relate your data structure to the flat file in I specification by renaming the file field (Same as object name) to the name of this data structure.
Now as soon as you read the flat file, business attributes are automatically populated as subfields of data structures.
- Method 2.– Since you now understand that flat file is just a special name of a single column physical file, you can create any such physical file using a DDS also. When we create a physical file, we generally keep the object, file and field names different.
In this method, we do not need the I-spec renaming. Instead we can write an Eval statement which populates the data structure ultimately populating the sub-fields (Business attributes).
On the next page we will see few RPG IV examples to process flat files.
- 3080 reads
