Hi,
I have written a program, which reads records from a program described file and write records to another program describe file. However it seems working but that doesn't seem an efficient program to me. Could anyone please suggest me what can be done to improve this program.
FEmployee IF F 39 DISK
FEmpoutf O F 39 DISK
Drecemp DS
DEMPID 1 10
DFNAME 11 20
DMNAME 21 21
DLNAME 22 31
DSALARY 32 35P 2
DDEPT 36 39
IEmployee NS
I 1 10 empid
I 11 20 fname
I 21 21 mname
I 22 31 lname
I P 32 35 2Salary
I 36 39 DEPT
/FREE
READ EMPLOYEE;
DOW NOT %EOF;
WRITE EMPOUTF RECEMP;
READ EMPLOYEE;
ENDDO;
EVAL *INLR = *ON;
RETURN;
/END-FREE
Oempoutf D
O EMPID 10
O FNAME 20
O MNAME 21
O LNAME 31
O SALARY 35P
O DEPT 39
Some doubts:
1. I had to define an extra data structure recemp as for program described files it's necessary to use the data structure name in WRITE operation. Now my doubt is, could i have defined data structure only once and use that data structure in I-specs and O-specs as my both Input and Output file structure is exactly same.
2. I have seen in some programs that after reading the record we dont move it to output record, while in COBOL we explicitly have to populate the output record before writing it to a file. How is this achieved in RPG?
3. Kindly suggest me more ways the program can be improved.
Regards
Shruti
Software/Hardware used:
ASKED:
April 3, 2009 10:26 AM
UPDATED:
April 3, 2009 2:51 PM
Ok, I dont know the use of CPYF in RPG program. I will search for it. Thanks.
In this particular example how can i populate RECEMP data structure (i.e how to move record from input file to the data structure) as there is no record format name of input file. I am so confused in this program described file…extarnally described file is a way easier than this….but i need to learn these concepts as well….kindly help…
Hi,
I think you’re making it a bit too complicated. The following should do the trick. Notice that I put an A for add in the file spec for empoutf and an ADD on the output spec. I changed the write to use EXCEPT and the D (detail time) to and E (exception time) on the output spec. I removed the D specs completely and replaced the input field definitions with one field for the whole length of the record – the field is also used in the output specs.
FEmployee IF F 39 DISK
FEmpoutf O A F 39 DISK
IEmployee NS
I 1 39 data
/FREE
READ EMPLOYEE;
DOW NOT %EOF;
EXCEPT EMPOUTF;
READ EMPLOYEE;
ENDDO;
EVAL *INLR = *ON;
RETURN;
/END-FREE
Oempoutf EADD
O data 39
Hope this makes some sense to you. I can understand that the use of internally defined files can be quite confusing if you’re used to externally defined files. All this stuff comes from older (non-AS400) versions of RPG where externally defined files couldn’t be used.
If you come from a System/36 background like me, then internally defined files are almost second nature.
By the way CPYF is an AS/400 (CL) command. What I meant by using CPYF was that you didn’t really need to write a program at all for this. If you use CPYF you can copy data from one file to another – it’s a very powerful command which lets you select what data you want to copy, allows creation of a duplicate file if necessary, can clear existing data by using a replace option, etc.
Regards,
Martin Gilbert.
Hey thanks Martin, this looks quite simple and tidy now
I have used CPYF command quite many times, earlier I thought CPYF is some keyword in RPG also, using which file description can be copied….
In the given code, both the input file and output file are specified as flat file. Suppose the input data has a packed field in it, then also the program will work as expected? Won’t in abend?
I tried both ways, by defining the explicit fields in input specs and by defining the flat record structure, and it doesn’t abend….howcome? Issuing a read on this file means moving packed data to this flat structure….is this permissible?
Hi,
It doesn’t matter about the packed data as long as the file has no fields defined (flat file). If you tried to copy a flat file to an externally defined file you may get problems if for example you have blanks or character data in the position of a numeric or packed field. If you copy from an externally defined file to a flat file you won’t have any problems.
The OS checks data that gets copied into externally described files and will issue error messages if the data being copied is invalid for the file field definitions. Flat files only (normally) have one character field (often with the same name as the file, which means you’d have to use a RENAME in your RPG if you want to use the file as externally described). Packed data is allowed in character fields, be aware that if you query or dsppfm a file with packed data in a character field the packed data may not be shown unless you display in hex mode (which you can’t do in query).
Regards,
Martin Gilbert.
Thanks Martin for your help….it was described so well that i understood it easily….thx again..