HPlogo Using KSAM XL and KSAM 64 > Appendix A COBOL Intrinsics

CKREAD

MPE documents

Complete PDF
Table of Contents
Index

E0300 Edition 4 ♥
E0394 Edition 3

A call to procedure CKREAD makes available the next logical record from a KSAM file.

  CALL "CKREAD" USING filetable, status, record, recordsize

In order to read records in sequential order by key value, call procedure CKREAD. The file must have been opened in input or input/output mode with access mode specified as either sequential or dynamic.

Parameters


filetable

An 8 halfword record containing the number and name of the file, its input/output type, access mode, and a code indicating whether the previous operation was successful and if so, what it was.

status

One halfword (two 8-bit characters) set to a pair of values upon completion of the call to CKREAD to indicate whether or not the record was successfully read and if not, why not.

record

A record defined in the WORKING-STORAGE SECTION into which the contents of the next sequential KSAM record is read.

recordsize

An integer (S9(4)COMP) containing the length in characters of the record being read. It must not exceed the maximum record length established for the file when it was created.

Operation Notes


The file from which the record is read must be opened for sequential or dynamic access (access mode = 0 or 2). It may be opened for input only or input/output (input/output type = 0 or 2), but not for output only.

When the file is opened initially for input or input/output, the logical record pointer is positioned at the first sequential record; that is, at the record with the lowest key value. The key used is the primary key unless a previous call to CKSTART has specified an alternate key. When a call to CKREAD is executed, the record at which the record pointer is currently positioned is read into the location specified by record.

If, when CKREAD is executed, there is no next logical record in the file, the at end condition is returned to status; that is, status is set to 10. Note that a call to the procedure CKSTART can be used to reposition the pointer for subsequent sequential access according to primary or alternate key order.

In order to update records in sequential order, CKREAD must be called before executing either of the update procedures CKREWRITE or CKDELETE. When access is shared, it is important to include the call to CKREAD within the same locked portion of code that includes the call to CKREWRITE or CKDELETE. This ensures that the correct record is modified or deleted.

Because CKREAD is a pointer-dependent procedure, the actual record read depends on the current position of the logical record pointer. When access is shared, this pointer position can be made incorrect by other users without your program being aware of it. For this reason, you should lock the file, position the pointer with a pointer-independent procedure, and then call CKREAD. When the last record is read, you should then unlock the file so other users can access the file. Example 2 below illustrates how you should read the file sequentially when access is shared.

Using the WORKING-STORAGE SECTION from Figure A-2 "Representation of KSAMFILE Used in COBOL Examples" and the FINISH procedure in the CKCLOSE example, the following procedures read records in sequential order from file KSAMFILE and display them on the standard output device.

  PROCEDURE DIVISION.

  START.
  .
  .
  .
      MOVE 0 TO I-O-TYPE, A-MODE.
      CALL "CKOPEN" USING FILETABLE, STAT.
      IF STATUS-KEY-1 = "9"
          CALL "CKERROR" USING STAT, RESULT
          DISPLAY "CKOPEN ERROR NO. ", RESULT.
      IF STATUS-KEY-1 NOT = "0"
          DISPLAY "CKOPEN FAILED"
          STOP RUN.
  READ-NEXT.
      CALL "CKREAD" USING FILETABLE, STAT, REC, RECSIZE.
      IF STATUS-KEY-1 = "1" GO TO NEW-POSITION.
      IF STATUS-KEY-1 = "0"
          DISPLAY REC;
      ELSE
          DISPLAY "CKREAD ERROR, STATUS =", STAT.
      IF STATUS-KEY-1 ="9"
          CALL "CKERROR" USING STAT, RESULT
          DISPLAY "FILE ERROR =", RESULT.
      GO TO READ-NEXT.
  NEW-POSITION.
  .
  .
  .

The following example provides a sequential read with shared access.

  PROCEDURE DIVISION.
  START.
  .
  .
  .
      MOVE 0 TO I-O-TYPE, A-MODE.
      CALL "CKOPENSHR" USING <--- open file for shared access
        FILETABLE, STAT
  .
  . <--- test status
  .
  FIND-RECORD.
      MOVE 2 TO RELOP.
      MOVE "000-0000" TO KEYVAL.
      MOVE 23 TO KEYLOC,
      MOVE 8 TO KEYLENGTH.
      MOVE 1 TO LOCKCOND.
      CALL "CKLOCK" USING <--- lock file unconditionally
        FILETABLE, STAT, LOCKCOND.
      CALL "CKSTART" USING <--- position pointer to lowest key value
        FILETABLE, STAT, RELOP, KEYVAL, KEYLOC, KEYLENGTH.
  .
  . <--- test status
  .
  READ-RECORD.
      CALL "CKREAD" USING <--- read record
        FILETABLE, STAT, REC, RECSIZE
      IF STATUS-KEY-1 ="1" <--- end of file
          GO TO END-OF-READ.
      IF STATUS-KEY-1 ="0" <--- if successful, display record read
          DISPLAY REC.
  .
  . <--- test status for errors
  .
      GO TO READ-RECORD.
  END-OF-READ.
      CALL "CKUNLOCK" USING FILETABLE, STAT. <----- unlock file




CKOPENSHR


CKREADBYKEY