CKREAD [ KSAM/3000 Reference Manual ] MPE/iX 5.0 Documentation
KSAM/3000 Reference Manual
CKREAD
A call to procedure CKREAD makes available the next logical record from a
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-word 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. (Refer to Filetable Parameter
discussion earlier in this section.)
status one-word (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. (Refer to Status Parameter discussion
earlier in this section.)
record a record defined in the WORKING-STORAGE SECTION into
which the contents of the next sequential KSAM record is
read.
recordize an integer (S9(4)COMP) containing the length in
characters of the record being read. It must not exceed
maximum record length established for the file when it
was created.
USING CKREAD
The file from which the record is read must be open 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 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 and 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 insures that the correct record is modified
or deleted.
SHARED ACCESS. Because CKREAD is a pointer-dependent procedure (refer to
Table 3-3), 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
pointerdent 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.
EXAMPLE
Using the WORKING-STORAGE SECTION from Figure 3-2 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.
1. Example of Sequential Read
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.
.
.
. see CKSTART example
2. Example of Sequential Read with Shared Access
PROCEDURE DIVISION.
START.
.
.
.
MOVE 0 TO I-O-TYPE, A-MODE.
CALL "CKOPENSHR" USING FILETABLE, STAT <----- open file for shared access
.
.<----- 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 FILETABLE, STAT, LOCKCOND.<----- lock file unconditionally
CALL "CKSTART" USING FILETABLE,
STAT, RELOP, KEYVAL, KEYLOC, KEYLENGTH.<--- position pointer to lowest key value
.
. <----- test status
.
READ-RECORD.
CALL "CKREAD" USING FILETABLE, STAT, REC, RECSIZE<----- read record
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
.
TO TO READ-RECORD.
END-OF-READ.
CALL "CKUNLOCK" USING FILETABLE, STAT.<----- unlock file
MPE/iX 5.0 Documentation