 |
» |
|
|
|
|  |  |
PRIMARY KEY SEQUENCE |  |
Reading a file in primary key order requires no other preparation than to open the file (file system) or to open the file for sequential input (COBOL). In the file system, sequential logical read is the default and the aoptions parameter can be omitted from the FOPEN call. In a COBOL procedure, input type and sequential access are indicated by zero values in the appropriate words of the filetable table. Once opened for input, the file system FREAD intrinsic or the COBOL CKREAD procedure can be called to read the file in sequence by primary key. ALTERNATE KEY SEQUENCE |  |
To read a file in sequence by an alternate key, that alternate key must be specified in a call prior to the call to a read procedure or intrinsic. In COBOL, you would use a call to CKSTART; with the file system intrinsics you would use FFINDBYKEY. The example in Figure 5-3 “Reading KSAM File in Key Sequence Using FORTRAN” illustrates use of the file system intrinsics FREAD and FFINDBYKEY to read a KSAM file in sequence first by primary key and then by alternate key. RANDOM ORDER |  |
A particular record in the file can be selected for access according to the value of a key field in the record. This can be a primary or alternate key field. In COBOL, a call to CKREADBYKEY reads a record specified by the key value parameters. The file system uses the intrinsic FREADBYKEY for the same purpose. The main difference here is that the file must be opened for random access before calling the COBOL procedure; no distinction is made by the MPE file system between a file opened for sequential access and one opened for random access. Figure 5-3 Reading KSAM File in Key Sequence Using FORTRAN
C*****************************************************
C EXAMPL2 *
C READ KSAM FILE SEQUENTIALLY *
C*****************************************************
SYSTEM INTRINSIC FOPEN,FCLOSE,FWRITE,FERRMSG,FCHECK
SYSTEM INTRINSIC FWREAD,FFINDBYKEY
INTEGER FILNUM
CHARACTER FILENAME*10
CHARACTER OUTPUT*72
CHARACTER MESSAGE(72)
LOGICAL INPUT (36)
LOGICAL MESSAGEW(36)
EQUIVALENCE (MESSAGE,MESSAGEW)
EQUIVALLNCE (OUTPUT,INPUT)
CHARACTER KEYVAL*8
DATA FILENAME/"FEXAMFIL " /
DATA KEYVAL/"000-0000"/
C*****************************************************
C OPEN KSAM FILE FOR INPUT *
C*****************************************************
FILNUM=FOPEN(FILENAME,%7L)
IF (FILNUM .EQ. 0) GO TO 200
C*****************************************************
C READ DATA FROM FILE IN *
C SEQUENTIAL ORDER *
C*****************************************************
DISPLAY "PRINT RECORDS IN NAME ORDER"
20 ILEN=FREAD(FILNUM,TNPUT,-72)
IF (.CC.) 300,30,35
30 DISPLAY OUTPUT
GO TO 20
C*****************************************************
C READ IN SEQUENCE BY ALTERNATE KEY *
C*****************************************************
35 DISPLAY "PRINT RECORDS IN PHONE # ORDER"
CALL FFINDBYKEY(FILNUM,KEYVAL,21,8,2)
IF (.CC.) 400,40,400
40 ILEN=FREAD(FILNUM,INPUT,-72)
IF (.CC.) 500,45,500
45 DISPLAY OUTPUT
GO TO 40
C*****************************************************
C CLOSE FILE *
C*****************************************************
50 CALL FCLOSE(FILNUM,0,0)
IF (.CC.) 600,55,600
55 STOP "END OF JOB"
C*****************************************************
C ERROR MESSAGES *
C*****************************************************
200 CALL FCHECK(FILNUM,IERRNUM)
CALL FERRMSG(IERRNUM,MESSAGEW,LENGTH)
WRITE(6,250)(MESSAGE(I),I=1,LENGTH)
STOP "CANNOT OPEN KSAM FILE"
300 CALL FCHECK(FILNUM,IERRNUM)
CALL FERRMSG(IERRNUM,MESSAGEW,LENGTH)
WRITE(6,250)(MESSAGE(I),I=1,LENGTH)
400 CALL FCHECK(FILNUM,IERRNUM)
CALL FERRMSG(IERRMUN,MESSAGEW,LENGTH)
WRITE(6,250)(MESSAGE(I),I=1,LENGTH)
STOP " ERROR OCCURRED WHILE USING FIFINDBYKEY"
500 CALL FCHECK(FILNUM,IERRNUM)
CALL FERRMSG(IERRNUM,MESSAGEW,LENGTH)
WRITE(6,250)(MESSAGE(I),I=1,LENGTH)
STOP "ERROR OCCURRED READING BY ALTERNATE KEY"
600 CALL FCHECK(FILNUM,IERRNUM)
CALL FERRMSG(IERRNUM,MESSGEW,LENGTH)
WRITE(6,250)(MESSAGE(I),I=1,LENGTH)
STOP "CANNOT CLOSE FILE"
250 FORMAT(1X,72A1)
END
Output from Program Execution:
PRINT RECORDS IN NAME ORDER
CARDIN RICK 578-7018 11100 WOLFE ROAD CUPERTINO
ECKSTEIN LEO 287-5137 5303 STEVENS CREEK SANTA CLARA
HOSODA JOE 227-6214 1180 SAINT PETER CT. LOS ALTOS
NOLAN JACK 923-4975 967 REED AVE. SUNNYVALE
PASBY LINDA 295-1187 TOWN & CNTRY VILLAGE SAN JOSE
ROBERT GERRY 259-5535 12345 TELEGRAPH AVE. BERKELEY
SEELY HENRY 293-4220 1144 LEBERTY ST. EL CERRITO
TURNEWR IVAN 984-8498 22905 EMERSON ST. OAKLAND
WESTER ELDER 287-4598 1256 KINGFISHER ST. SUNNYVALE
WHITE GORDON 398-0301 4350 ASHBY AVE. BERKELEY
PRINT RECORDS IN PHONE # ORDER
HOSODA JOE 227-8214 1180 SAINT PETER CT. LOS ALTOS
ROBERT GERRY 259-5535 12345 TELEGRAPH AVE. BERKELEY
WESTER ELDER 287-4598 1256 KINGFISHER ST. SUNNYVALE
ECKSTEIN LEO 287-5l37 5303 STEVENS CREEK SANTA CLARA
SEELY HENRY 293-4220 1144 LEBERTY ST. EL CERRITO
PASBY LINDA 295-1187 TOWN & CNTRY VILLAGE SAN JOSE
WHITE GORDON 398-0301 4350 ASHBY AVE. BERKELEY
CAPDIN RICK 578-7018 11100 WOLFE ROAD CUPERTINO
NOLAN JACK 923-4975 967 REED AVE. SUNNYVALE
TURNEWR IVAN 984-8498 22905 EMERSON ST. OAKLAND
STOP END OF JOB
|
|