Wednesday, December 4, 2013

Day-8.


 Overview:
This module enables the student to understand ‘F’ Specifications and READ and WRITE opcodes.
 Outline:
Topics covered,
  • Details of ‘F’ Specifications
  • Use of two physical files.
  •   How to read from physical file.
  • How to write in Physical file.
  •      How to call RPG program in CL.
  Objective:
By the end of this module, the student should be able to Understand:
  • Knowing F Specification.
  • How to code RPG program by defining PF file.
  • How to run RPG program in CL program.
 File Description Specifications

F Specification line is broken into following parts.

Positions
Name
Entry
1-2
Page
Page number
3-5
Line
Line number
6
Form type
F
7-14
File name
Valid file name
15
File type
I
O
U
C
16
File designation
Blank
P
S
R
T
F
17
End of file
E
Blank
18
Sequence
A or Blank
D
19
File format
F
E
20-23

Blank
24-27
Record length
1-9999
28
Limits processing
L
Blank
29-30
Length of key field or record address field
1-99
Blank
31
Record address type
Blank
A
P
32
Type of file organization
Blank
I
T
33-34
Overflow indicators
Blank
OA-OG, V
35-38
Key field starting location
Blank
1-9999
39
Extension code
Blank
E
L
40-46
Device
PRINTER
DISK
WORSTN
SPECIAL
SEQ
47-52

Blank

53
Continuation lines
Blank
K
54-59
Name of routine
Name of user-supplied routine
60-65

Blank

66
File addition/unordered
Blank
A
67-70

Blank
71-72
File condition
Blank
U1-U8
73-74

Blank
75-80
Comments
Optional

File description specifications describe all the files that your program uses. The information of each file includes:
·         Filename: Name of the file
·         File Type:
ü  Input (I)         :  Type I to indicate an input file.
ü  Output (O)        :  Type O to indicate an output file.
ü  Update (U)       :   Type U to indicate an update file.
ü  Combine(C)     :   Type C to indicate a combined (input/output) file.
                                                     
·         File Designation:
ü  Blank (b)                       :Leave the field blank to  indicate an output file.
ü  Fully Procedural (F)        :Type F to indicate a Full procedural file.
ü  Primary File (P)              :Type P to indicate a primary file                         
ü  Record Address file (R)  :Type R to indicate a Record Address File.
ü  Secondary File (S)         :Type S to indicate a secondary file.                         
ü  Array or Table File (T)   : Type T to indicate an array or  table file.                  

·         File Format:
ü  Externally described (E) : Type E to indicate an externally described file.
ü  Program described (F)   : Type F to indicate a program-described file.

·         Record Length: Length of record for program described file (maximum 9999)

·         Record Address Type:
ü  Sequential Processing (b)
ü  Program described File key field charecter. (A)
ü  Program described File key field packed decimal (P)
ü  Externally described file (K) indicates keyed file.

·         Device file:
o    WORKSTN  :        The file is a workstation file.
o    SPECIAL     :        The file is a special file.
o    PRINTER    :        The file is a PRINTER file.
o    DISK          :        The file is a DISK file.  
o    SEQ           :         The file is a sequentially organized file.


1) Create ‘STUD1’ physical file having 2 fields as shown below.

Columns . . . :    1  71            Edit                        PB01U01D/MYDDS
 SEU==>                                                                   STUD1
 FMT PF .....A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
        *************** Beginning of data *************************************
0001.00      A         R STDR                                                 
0002.00      A             ROLL           3S 0       COLHDG('ROLL NUMBER')      
0003.00      A            SNAME          5A        COLHDG('STUDENT NAME')     
0007.00      A         K ROLL                                                 
        ****************** End of data ****************************************
                                                                                                                                                  
                                                                               
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                
                                         (C) COPYRIGHT IBM CORP. 1981, 2002.   

2) Create RPG program having name ‘RPG1’ & specify or declare file as shown below

Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT H  .....H........1..CDYI....S..............1.F............................
IPF     *************** Beginning of data *************************************
        ****************** End of data ****************************************
                                                                               
                                                                                
                                                                            
                                                                               
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                
                                                                               
NOTE : ‘IPF’ stands for Input prompt for FILE.
  
PRESS ENTER
Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT H  .....H........1..CDYI....S..............1.F............................
        *************** Beginning of data *************************************
'''''''                                                                        
        ****************** End of data ****************************************
 Prompt type . . .    F      Sequence number . . .  '''''''                    
                                                                               
               File        File          End of                   File         
 Filename      Type     Designation       File      Sequence     Format        
                                                                               
 Record      Mode of        Length of         Record                           
 Length     Processing      Key Field      Address Type                         
                                                                               
     File            Overflow       Key Field      Extn                        
 Organization        Indicator      Start Loc      Code     Device             
                                                                                
                                         File            File                  
 Continuation     Exit     Entry      Addition         Condition               
                                                                                
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                       
                                                                               

For defining file user has to enter ‘FILE NAME’, ‘FILE TYPE’, ‘FILE DESIGNATION’, ‘FILE FORMAT’,’REOCORD ADDRESS TYPE’, and ‘DEVICE’ as shown below. As we declaring physical file so we have to define ‘Record Address Type’ and ‘Device’. For PF we declare device of physical file as a DISK device.

Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT F  .....FFilenameIPEAF....RlenLK1AIOvKlocEDevice+......KExit++Entry+A....U
        *************** Beginning of data *************************************
'''''''      FSTUD1   IF  E           K        DISK                            
        ****************** End of data ****************************************
 Prompt type . . .    F      Sequence number . . .  '''''''                    
                                                                                
               File        File          End of                   File         
 Filename      Type     Designation       File      Sequence     Format        
 STUD1          I           F                                      E           
 Record      Mode of        Length of         Record                           
 Length     Processing      Key Field      Address Type                        
                                                K                               
     File            Overflow       Key Field      Extn                        
 Organization        Indicator      Start Loc      Code     Device             
                                                            DISK                
                                         File            File                  
 Continuation     Exit     Entry      Addition         Condition               
                                                                                
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                       
 
 
After inserting the field according to above sequence press enter 2 times to get the following screen.

Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT F  .....FFilenameIPEAF....RlenLK1AIOvKlocEDevice+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FSTUD1   IF  E           K        DISK                            
        ****************** End of data ****************************************
                                                                               
                                                                               
                                                                                                                                                        
                                                                                
                                                                               
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle       
 F16=Repeat find       F17=Repeat change          F24=More keys                

Now we are going to read the content of ‘STUD1’ with ‘READ’ keyword and make it display on the screen using DSPLY keyword.

To proceed further make use of ‘C’ specification. 

Read (Read a record)

Read operation retrieves record sequentially from a full procedural file . Record format or file name  is written in the factor2 of the file that is to be read. Indicators define in the EQ (EOF) factor get on if record is not found. Indicators define in the LO (ER) factor get on if READ operation was not completed successful.

Factor 1

  

Factor 2

   File name / record format (Mandatory)

Result Field

   Data Structure (only for program describe file)

HI


LO

   ER (Optional)

EQ

EOF








So read the file as shown below.



Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FSTUD1   IF  E           K        DISK                            
0003.03       * READ FILE                                                                
0003.05      C                     READ STDR                     10            
        ****************** End of data ****************************************
                                                                                
                                                                               
                                                                               
                                                                                
                                                                               
 Prompt type . . .    C      Sequence number . . .  0003.05                    
                                                                               
 Level    N01N02N03  Factor 1     Operation      Factor 2     Result           
‘STUD1’ Record Format’ name
 
                                    READ         STDR                          
             Decimal                                                           
 Length     Positions    H/N/P   HI   LO   EQ    Comment                       
                                           10                                  
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                       

Hit enter which gives you a blank prompt of ‘C’ spec to do a new entry make entry as shown below.(‘ROLL’ and ‘SNAME’ are our fields from ‘STUD1’ physical file).

Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FSTUD1   IF  E           K        DISK                            
0003.03       * READ FILE                                                                
0003.05      C                     READ STDR                     10            
0004.00      C           ROLL      DSPLY                                       
0004.01      C           SNAME     DSPLY                                        
0005.00      C                     SETON                     LR                
        ****************** End of data ****************************************
                                                                               
                                                                                                                                             
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                
                                         (C) COPYRIGHT IBM CORP. 1981, 2002.   
 
Above ‘DSPLY’ shows only first record of PF file. So to define all records from ‘STUD1’ we have to use ‘DO Loop’ as shown below.
So we will make use of ‘Indicator’ which is define with read operation. So to define indicator we use ‘IN**’ (** means the indicator which we define i.e. *IN10).

There are 2 types of DO loop
  1. DO-While Loop (DOW** – ENDDO) : This loop checks the condition before execution.
  2. DO-Until Loop (DOU** – ENDDO) : This loop checks the condition after execution.

We use to define DO** loop in opcodes. (** means ‘Relational Operator’ )
EQ : EQual To
LE :  Less than Equal to
GT : Greater Than
GE : Greater than Equal to
NE : Not Equal

So define ‘DO – Loop’ as shown below.
Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FSTUD1   IF  E           K        DISK                             
0003.03       * READ FILE                                                               
0003.05      C                      READ STDR                     10            
0005.00      C       *IN10     DOWEQ*OFF                                    





Prompt type . . .    C      Sequence number . . .  0005.00                    
                                                                               
 Level    N01N02N03  Factor 1     Operation      Factor 2     Result            
                       *IN10         DOWEQ        *OFF                          
             Decimal                                                           
 Length     Positions    H/N/P   HI   LO   EQ    Comment                       
                                                                                
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                       
 
Now use ‘DSPLY’ keyword to display record and complete the program as shown below.
                                                                              
Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FSTUD1   IF  E           K        DISK                            
0003.03       *READ FILE                                                                
0003.05      C                     READ STDR                     10            
0005.00      C           *IN10     DOWEQ*OFF                                   
0006.00      C           ROLL      DSPLY                                       
0007.00      C           SNAME     DSPLY                                        
0007.04      C                     READ STDR                     10   
0007.05       * Write into File                                                                        
0007.06      C                     WRITERSTD                                    
0008.00      C                     ENDDO                                       
0009.00      C                     SETON                     LR                
        ****************** End of data ****************************************
                                                                               
                                                                               
                                                                               
                                                                                                                                                           
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                
Above program shows you the whole record from physical file.

Now in next step we have to copy or move content of one PF ‘STUD1’ to ‘STUD2’ so that you have to create a new physical file ‘STUD2’ as shown below,

  • Create ‘STUD2’ physical file having 2 fields as shown below.

Columns . . . :    1  71            Edit                        PB01U01D/MYDDS
 SEU==>                                                                   STUD2
 FMT PF .....A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
        *************** Beginning of data *************************************
0001.00      A          R RSTD                                                 
0002.00      A             ROLLNO       3S 0      COLHDG('ROLL NUMBER')      
0003.00      A             NAME           5A         COLHDG('STUDENT NAME')     
0007.00      A          K ROLLNO                                                
        ****************** End of data ****************************************
                                                                               
                                                                                
                                                                               
                                                                                                                                   
                                                                                
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                
                                         (C) COPYRIGHT IBM CORP. 1981, 2002.   

  • In second step define ‘STUD2’ in file(F) spec as shown below and press enter, put information of file as shown below. And read that file in C-Specification.

Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FSTUD1   IF  E           K        DISK          
0002.00      FSTUD2   UF  E         K        DISK                      A      
0003.03       *READ FILE                                                                 
0003.05      C                     READ STDR                     10  
0004.00      C                     READ RSTD                     10                      
0005.00      C      *IN10     DOWEQ*OFF                                   
0006.00      C     ROLL      DSPLY                                       
0007.00      C     SNAME   DSPLY                                       
0007.04      C                      READ STDR                     10            
0007.05       * Write into File                                                                        
0007.06      C                     WRITERSTD                                   
0008.00      C                     ENDDO                                       
0009.00      C                     SETON                     LR                
        ****************** End of data ****************************************
                                                                               
                                                                                
                                                                                                                                                     
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                

NOTE :We are updating ‘STUD2’ so that we define FILE TYPE as ‘U’ and  FILE ADDITION ‘A’ which helps us to add records in ‘STUD2’ . File name must be less than or equal to 6 character only.
To move the content of ‘STUD1’ to ‘STUD2’ use Opcodes ‘MOVE’ and ‘Z-ADD’ for Character and Numeric field respectively.  And use them in program as shown below.



Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FSTUD1   IF  E           K        DISK                             
0001.01      FSTUD2   UF  E          K        DISK                      A     
0003.03       *                                                                
0003.05      C                      READ STDR                     50             
0003.06      C                      READ RSTD                     20            
0005.00      C       *IN50     DOWEQ*OFF                                   
0006.00      C       ROLL      DSPLY                                       
0007.00      C    SNAME     DSPLY                                       
0007.01      C                       Z-ADDROLL      ROLLNO                       
0007.03      C                       MOVE SNAME     NAME                         
0007.04      C                       READ STDR                     50            
0008.00      C                       ENDDO                                       
0009.00      C                       SETON                     LR                
        ****************** End of data ****************************************
                                                                               
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                

Above program only moves the fields. To write the field in ‘STUD2’ we used ‘WRITE’ opcodes.
                             

WRITE (Write to file)

The write operation must designate a record format name rather than a file name. If this ‘write’ operation is adding a record to the file, which already contains records, then ‘A’ in position 66 of File Specification for that file is required. Indicator defile in the LO entry set on if the WRITE operation is not completed successfully.

Factor 1

  

Factor 2

   File name / record format (Mandatory)

Result Field

   Data Structure (only for program describe file)

HI

 

LO

   Er

EQ
















To write the content in PF use WRITE opcodes with ‘STUD2’ record format ‘RSTD’ in Factor2 as shown below. 

Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                    RPG1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FSTUD1   IF  E           K        DISK                             
0001.01      FSTUD2   UF  E          K        DISK                      A     
0003.03       * READ FILE                                                               
0003.05      C                      READ STDR                     50            
0003.06      C                      READ RSTD                     20            
0005.00      C        *IN50   DOWEQ*OFF                                   
0006.00      C      ROLL      DSPLY                                       
0007.00      C    SNAME     DSPLY                                       
0007.01      C                       Z-ADDROLL      ROLLNO                       
0007.03      C                       MOVE SNAME     NAME                         
0007.04      C                       READ STDR                     50            
0007.05       * Write into File                                                                        
0007.06      C                       WRITERSTD                                   
0008.00      C                        ENDDO                                       
0009.00      C                        SETON                     LR                
        ****************** End of data ****************************************
                                                                                
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                

Now compile above program using option ‘14’ to create its object. This program transfers the content of STUD1 to STUD2. After creation of object run the program using CL command ‘CALL <filename>’ on command line to see the output. (i.e. CALL RPG1).

Every time when we call this program it gets the record from one PF ‘STUD1’ and insert to another PF ‘STUD2’ at that time same record gets added or append in STUD2 not to overwrite. So for clearing the content of ‘STUD2’ before inserting the record again we have to write small CL program which clears the Content of ‘STUD2’ and before writing the data in that file.

Create the CL program having
Program Name : CL1
Source Type : CLP
Text : ‘Program for clearing Physical File Member’
Columns . . . :    1  71            Edit                        PB01U01S/MYCLP
 SEU==>                                                                    CL1
 FMT **  ...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 
        *************** Beginning of data *************************************
0001.00 PGM:                                                                   
0002.00              CLRPFM     FILE(PB07U01O/STUD2)                           
0002.01              CALL       PGM(RPG1)                                      
0003.00 ENDPGM:                                                                
        ****************** End of data ****************************************
                                                                                
                                                                               
                                                                               
                                                                                
                                                                               
                                                                               
                                                                                
                                                                               
                                                                               
                                                                                
                                                                               
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                
                                         (C) COPYRIGHT IBM CORP. 1981, 2002.   



‘CLRPFM’ is the command, which clears the content of ‘STUD2’ & calling of RPG1 helps to move the data of ‘STUD1’ to ‘STUD2’.

Compile the above program using option ‘14’ and call it from command line (i.e. CALL CL1). After compilation just check ‘STUD2’ using CL command RUNQRY *N STUD2.



No comments:

Post a Comment