Using Subfiles to Minimize I/O Operations
You can use subfiles to read or write a number of records to and from a display in one operation.
Example:
The following subfile example uses DDS from T1520DDG and T1520DDH to display a list of names and telephone numbers.
- To create the display file T1520DDG using the DDS source shown below, enter:
CRTDSPF FILE(MYLIB/T1520DDG) SRCFILE(QCPPLE/QADDSSRC)Figure 1. T1520DDG — DDS Source for a Subfile Display A DSPSIZ(24 80 *DS3) A R SFL SFL A NAME 10A B 10 25 A PHONE 10A B +5 A R SFLCTL SFLCTL(SFL) A SFLPAG(5) A SFLSIZ(26) A SFLDSP A SFLDSPCTL A 22 25'<PAGE DOWN> FOR NEXT PAGE' A 23 25'<PAGE UP> FOR PREVIOUS PAGE' - To create the physical file T1520DDH using the DDS source shown below, enter:
CRTPF FILE(MYLIB/T1520DDH) SRCFILE(QCPPLE/QADDSSRC)R ENTRY NAME 10A PHONE 10A - Type the following data into T1520DDH:
David 435-5634 Florence 343-4537 Irene 255-5235 Carrie 747-5347 Michele 634-4557 - To create the program T1520SUB using the source shown below, enter:
CRTBNDC PGM(MYLIB/T1520SUB) SRCFILE(QCPPLE/QACSRC)/* This program illustrates how to use subfiles. */ #include <stdio.h> #include <stdlib.h> #include <recio.h> #define LEN 10 #define NUM_RECS 20 #define SUBFILENAME "*LIBL/T1520DDG" #define PFILENAME "*LIBL/T1520DDH" typedef struct{ char name[LEN]; char phone[LEN]; }pf_t; #define RECLEN sizeof(pf_t) void init_subfile(_RFILE *, _RFILE *); int main(void) { _RFILE *pf; _RFILE *subf; /* Open the subfile and the physical file. */ if ((pf = _Ropen(PFILENAME, "rr")) == NULL) { printf("can't open file %s\n", PFILENAME); exit(1); } if ((subf = _Ropen(SUBFILENAME, "ar+")) == NULL) { printf("can't open file %s\n", SUBFILENAME); exit(2); } /* Initialize the subfile with records from the physical file. */ init_subfile(pf, subf);/* Write the subfile to the display by writing a record to the */ /* subfile control format. */ _Rformat(subf, "SFLCTL"); _Rwrite(subf, "", 0); _Rreadn(subf, "", 0, __DFT); /* Close the physical file and the subfile. */ _Rclose(pf); _Rclose(subf); } void init_subfile(_RFILE *pf, _RFILE *subf) { _RIOFB_T *fb; int i; pf_t record; /* Select the subfile record format. */ _Rformat(subf, "SFL"); for (i = 1; i <= NUM_RECS; i++) { fb = _Rreadn(pf, &record, RECLEN, __DFT); if (fb->num_bytes != EOF) { fb = _Rwrited(subf, &record, RECLEN, i); if (fb->num_bytes != RECLEN) { printf("error occurred during write\n"); exit(3); } } } }This program uses
_Ropen()to open subfile T1520DDG and physical file T1520DDH. The subfile is then initialized with records from the physical file. Subfile records are written to the display using the_Rwrited()function. - To run the program T1520SUB and see the output, enter:
CALL PGM(MYLIB/T1520SUB)David 435-5634 Florence 343-4537 Irene 255-5235 Carrie 747-5347 Michele 643-4557 <PAGE DOWN> FOR NEXT PAGE <PAGE UP> FOR PREVIOUS PAGE