How to write flat file data into pf?

185 pts.
AS400 physical file
Flat files
Physical File
Hi,,  M25 is one of my flat file which is 34 char length. I want to write data from flat file to normal pf. Pf contains 3 char field which is equally devide.

Now generaly to read file we have to requre key field but flat file doesn't have any key field then how i will write this pf? 



Answer Wiki

Thanks. We'll let you know when a new response is added.

You define the input file within your RPG Specs
Then read through the file moving data to the fields in the output file and do a WRITE
Here is some sample code
FOutFile O E Disk
IM25 AA 01
I 1 34 Rec
Read M25;
DoU %EOF(M25);
Ouput = Rec;
Read M25;
Write OutFileR;
*InLr = *On;


If your flat file is 34 characters wide, and your externally-described file is also 34 characters wide and has its field definitions lined up with the appropriate positions in the flat file, you use the CPYF command with the FMTOPT( *NOCHK ) parameter value set.

FMTOPT( *NOCHK ) tells CPYF to copy record images without checking field constraints.


Discuss This Question: 4  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • DanTheDane
    Several options are vailale to do what you want. Tom mentiond two of them. I'll supply you with 2 other options: 1. Use Query/400 over your flat file, and define result fields within the query using the fieldnames and field attributes from the target pf (SUBSTR from flat file). Then select Output type as '3' ('Database file'), whereafter you will be prompted for the name and lib of the outputfile ; - use '4' or '5' to replace/add to member. Having created the query, use RUNQRY command in your application-CL. That's it.. 2. Use RPG cycle. Using Tom's RPG code as model, here is my version, which uses the RPG-cycle for automatic reading a file from start till end ('ip' (input primary)). Thus there is no need for any more RPG-code to readthe file and end the program - the 'cycle' does just that.
    fM25       ip   f   34        disk
    foutfile   o  a e             disk
    iM25       aa  01
    i                                  1   34  Rec
         F1    = %subst(Rec: 1:11);
         Fn    = %subst(Rec:12:11);
         Write OutFileR;
    Since you'l propably need to move data to fields, I inserted some lines assuming fieldnmes in targetfile to be F1, F2 etc. It is often claimed that this method is executing faster tha doing your on read. Well, - I did some tests (timestamp added to outputrec) on this and must report that on a 10+ mio recs inputfile, the IP-model used 82 secs whereas Tom's READ-model used 80 secs. I recommend you to use the READ-model, since this code is propably more clear to maintain for the future, even though I (being an oldtimer) is still fascinated by the fact that the IP-model reduces needed coding. DanF
    2,555 pointsBadges:
  • TomLiotta
    To clarify, CharlieBrowne responded first with the RPG suggestion. I only added the CPYF comments. Tom
    125,585 pointsBadges:
  • DanTheDane
    Sorry CharlieBrowne, I overlooked that you were the source. Thanks to Tom for the info :-) DanF
    2,555 pointsBadges:
  • Brijesh

    Two methods to create flat files on AS400 system.

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

    2. 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).

    14,420 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: