Write User Input to Physical File | WRITE Opcode in Action
Write to a Database file (Physical File)
On the previous page we learned how to take user input and process it. In this RPGIV tutorial we will learn how to save all the calculations done by user on screen in a physical file.
For this, We will define a subroutine #WriteToDb (Write to Database) in the previous program. Also, we need to declare the physical file in Output mode. Output mode, because we only want to write to database file. File declaration should be easy now having learned about them on the very first page on File Processing in RPGIV.
Anyway, to write a record to a database file, we need to first populate the file fields. Once the fields have been populated we use the WRITE opcode to write to the database file. We have to specify the record format name of the output file as the factor 2 of WRITE opcode. The record format name as you can see from the source of the physical file is LOGPF01.
The complete source codes of display file, physical file and the RPG ILE program are given below.
FFilename++IPEASF.....L.....A.Device+.Keywords++++++++++ FLOGFILE O E Disk FLOGDSPF CF E WorkStn ** CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result C ExFmt LOG01 C DoW Not *INKA And $User <> *Blanks C ExFmt LOG02 ** C If Not *INKA C Eval $Sum = $Num1 + $Num2 ** C ExSr #WriteToDb ** C EndIf ** C EndDo ** C Eval *InLr = *On C Return ** Subroutine to keep logs of the summation C #WriteToDb BegSr C Eval user = $User C Eval num1 = $Num1 C Eval num2 = $Num2 ** Write to the database file C Write LOGPF01 C EndSr
** The physical file A R LOGPF01 A USER 10A A NUM1 15P 0 A NUM2 15P 0 A K USER // Display File A DSPSIZ(24 80 *DS3) A R LOG02 ** FUNCTION KEY DEFINED A CF01 ** SCREEN FIELDS AND LABELS A R LOG01 A CF01 A 11 19'Please Enter Your Name :' A $USER 10 B 11 45 A 23 3'F1=Exit' A COLOR(BLU) ** A 5 2'Enter two Numbers and Press Ent A to get their sum.' A 7 2'Enter First Number :' A $NUM1 10 0B 7 23 A 8 2'Enter Second Number:' A $NUM2 10 0B 8 23 A 10 23'-----------' A 11 17'Sum =' A $SUM 11 0O 11 23COLOR(WHT) A 23 2'F1=Exit' A COLOR(BLU) A 2 2'Sum Of Two Numbers' A COLOR(WHT) A DSPATR(UL)
Output of the RPGIV Program The output of the RUNQRY done on the pysical file after one run of the above program is in a screenshot.
Run the above program a number of times to write enough records in the database file. We will read in the next section how to retrieve all the records of the database file!
Note:- The above RPGILE program writes all the records to the database file. Even the blank ones! For this we can validate the program and display a suitable error message to the user.
- 4804 reads
