HPlogo KSAM/3000 Reference Manual: HP 3000 MPE/iX Computer Systems > Chapter 5 USING KSAM FILES IN FORTRAN PROGRAMS

CREATING A KSAM FILE WITH A CALL TO FOPEN

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

A KSAM file can be created with the >BUILD command of the KSAMUTIL program or it can be created programmatically through a call to the file system intrinsic FOPEN.Figure 5-1 “Creating and Writing to KSAM File in FORTRAN” contains a FORTRAN program that uses the intrinsic FOPEN to create and open a Ele, and the intrinsic FWRITE to write to the open file. It checks for errors with the FCHECK and FERRMSG intrinsics, and closes the file with a call to FCLOSE.

The file is named FEXAMFIL and the associated key file is named FKEYFILE. Two keys are used, a primary key of 20 characters starting in byte 1 of each data record, and an alternate key of eight characters starting in byte 21 of the data record. The primary key contains a name, the alternate key a phone number (refer to the input data in Figure 5-1 “Creating and Writing to KSAM File in FORTRAN”).

DEFINING KSAMPARAM

The parameter ksamparam describes the key file in an array that contains many different types of data (refer to Table 4-7 “FOPEN aoptions Parameter Format”). Because the data differs, the EQUIVALENCE statement is used to equate the word-array KSAMPARAMA to the byte-array KSAMPARAM to the double-word-array KSAMPARAMD. The keyfile name is in the first eight bytes and this is equivalenced to the beginning of the array. The key device is defined in word 7 of KSAMPARAMA, and the key descriptions begin in word 18.

The flag word (word 17) has the octal value 2. This means that only bit 14 is set to 1. The flagword defines the following options for the KSAM file:

bit 13 = 0

file is permanently saved in system directory

bit 14 = 1

record numbers in file start with 1, not zero

bit 15 = 0

records can be written in random order

If you compare this ksamparam definition to that in the SPL sample program (Title not available), you will note that the index values into the array differ. This is because, SPL arrays begin numbering with zero whereas FORTRAN arrays begin numbering with one.

CALLING FOPEN

In the FOPEN call, the first parameter is the KSAM file name that identifies the data file and the KSAM file as a whole. The second parameter specifies the file options (foptions) parameter as octal 4004:

[f0501a]

This defines the following file options:

   
New KSAM file(bit 4=1)
Allow :FILE(bit 5=0)
Fixed-Length Records (bits 8,9=00)
ASCII code(bit 13=1)
New file(bits 14,15=00)

Figure 5-1 Creating and Writing to KSAM File in FORTRAN


C***********************************************************************
C                                                                      *
C                           EXAMPLE 1 *                                *
C                      BUILD A KSAM FILE *                             *
C                                                                      *
************************************************************************
          SYSTEM INTRINSIC FOPEN,FCLOSE,FWRITE,FERRMSG,FCHECK 
          INTEGER KSAMPARAMA(26) 
          INTEGER KEYDESCRIPTION(8) 
          CHARACTER KSAMPARAM(52) 
          INTEGER*4 KSAMPARAMD(13) 
          CHARACTER*8 KEYFILENAME 
          CHARACTER*17 KEYDEVICEE 
          EQUIVALENCE ((KSAMPARAMA,KSAMPARAM,KSAMPARAMD,KEYFILENAME) 
          EQUIVALENCE (KEYDEVICE,KSAMPARAMA(7)) 
          EQUIVALENCE (KSAMPARAMA(18),KEYDESCRIPTION) 
          INTEGER FILNUM 
          INTEGER LENGTH 
          CHARACTER FILENAME*10 
          CHARACTER DEVICE*10 
          CHARACTER*72 INPUT 
          LOGICAL OUTPUT(36) 
          CHARACTER MESSAGE(72) 
          EQUIVALENCE MESSAGEW(36) 
          EQUIVALENCE (MESSAGE,MESSAGEW) 
          EQUIVALENCE (INPUT,OUTPUT) 
          DATA FILENAME/"FEXAMFIL  "/ <---------- filename 
          DATA DEVICE/"DISC  "/ device 
          DATA KEYDEVICE/"DISC  "/ device 
          DATA KEYFILENAME/"FKEYFILE" 
          DATA KSAMPARAMD(3)/100J/ <-------------- file size 
          DATA KSAMPARAMA(16)/2/ <---------------- flagword 
          DATA KSAMPARAMA(17)/2/ <--------------------- no. of keys 
          DATA KEYDESCRIPTION/%{4/1,12/20], 1,%[1/0,15/4],0,   key 
         1                    %[4/1,12/ 8],21,%[1/0,15/4], 0/ descriptions 
C***********************************************************************
C                                                                      *
C                      OPEN THE KSAM FILE                              *
C                                                                      *
C***********************************************************************
        FILNUM=FOPEN(FILENAME,*4004L,%101L,-72,DEVICE,KSAMPARAM,M, 
       1              ,10,,100J) 
        IF (FILNUM .EQ. 0)  GO TO 400 

************************************************************************
C                                                                      *
C                       READ DATA FROM $STDIN                          *
C                                                                      *
************************************************************************
         20 READ (5,300,END=30,ERR=40)   INPUT 
************************************************************************
C                                                                      *
C           WRITE THE DATA JUST READ TO THE KSAM FILE                  *
C                                                                      *
C***********************************************************************
          50 DISPLAY INPUT 
                    CALL FWRITE(FILNUM,OUTPUT,-72,%0L) 
                    IF (.CC.) 70,20,70 

C************************************************************************
C                                                                      *
C                            ERROR MESSAGE                             *
C                                                                      *
C***********************************************************************

          70 STOP "ERROR OCCURRED WHILE WRITING KSAM FILE" 
         100 STOP  "END OF JOB" 
          30 CALL FCLOSE(FILNUM,0,0) 
             IF (.CC.) 33,100,33 
          33 STOP "CAN NOT CLOSE THE KSAM FILE" 
          40 STOP "ERROR OCCURRED WHILE READING INPUT" 
         400 CALL FCHECK (FILNUM,IERRNUM) 
             CALL FERRMSG (IERRNUM,MESSAGEW,LENGTH) 
             WRITE(6,200) (MESSAGE(I),I=1,LENGTH) 
             STOP "CAN NOT OPEN KSAM FILE" 
         300 FORMAT(A72) 
         200 FORMAT(1X,72A1) 
             END 

             Output from Program Execution: 

          NOLAN     JACK    923-4975  967 REED AVE.          SUNNYVALE 
          HOSODA    JOE     227-8214  1180 SAINT PETER CT.   LOS ALTOS 
          ECKSTEIN  LEO     287-5137  5303 STEVENS CREEK     SANTA CLARA 
          CARDIN    RICK    578-7018  11100 WOLFE ROAD       CUPERTINO 
          PASBY     LINDA   295-1187  TOWN & CNTRY VILLAGE  SAN JOSE 
          SEELY     HENRY   293-4220  1144 LEBERTY ST.       EL CERRITO 
          ROBERT    GERRY   259-5535  12345 TELEGRAPH AVE.   BERLELEY 
          TURNEWR   IVAN    984-8498  22905 EMERSON ST.      OAKLAND 
          WHITE     GORDON  398-0301  4350 ASHBY AVE.        BERKELEY 
          WESTEP    ELDER   387-4598  1256 KINGFISHER ST.    SUNNYVALE 
STOP END OF JOP 

The next parameter defines the access options (aoptions) as the octal value 101:

[f0501b]

This defines the following access options:

  
KSAM access expected(bit 3=0)
Exclusive access(bits 8,9=01)
No dynamic locking(bit 10=0)
Write only access(bits 12-15=0001)

A new file contains no information and is always opened for write access. Before accessing the file for reading or update, it must be reopened. Such an open specifies that the file is an old file in the foptions parameter. Depending on the type of access expected, aoptions can be omitted or can specify a particular access type.