Format
#include <recio.h>
_RIOFB_T *_Rreadk(_RFILE *fp, void *buf, size_t size,
int opts, void *key, unsigned int keylen);
Language Level: ILE C Extension
Threadsafe: Yes. However, if the file pointer is passed among threads, the I/O feedback area is shared among those threads.
Description
The _Rreadk() function reads the record in the keyed access path that is currently being used for the file that is associated with fp. Up to size number of bytes are copied from the record into buf (move mode only). If the file is opened for updating, the _Rreadk() function locks the record positioned to unless __NO_LOCK is specified. You must be processing the file using a keyed sequence path.
The following parameters are valid for the _Rreadk() function.
The positioning options are mutually exclusive.
The following options may be combined with the positioning options using the bit-wise OR (|) operator.
The _Rreadk() function is valid for database and DDM files.
Return Value
The _Rreadk() function returns a pointer to the _RIOFB_T structure associated with fp. If the _Rreadk() operation is successful the num_bytes field is set to the number of bytes transferred from the system buffer to the user's buffer (move mode) or the record length of the file (locate mode). The key and rrn fields will be updated. The key field will always contain the complete key if a partial key is specified. When using record blocking with _Rreadk(), only one record is read into the block. Thus there are zero records remaining in the block and the blk_count field of the _RIOFB_T structure will be updated with 0. The blk_filled_by field is not applicable to _Rreadk() and is not updated. If the record specified by key cannot be found, the num_bytes field is set to zero or EOF. If you are reading a record by a partial key, then the entire key is returned in the feedback structure. If it is unsuccessful, the num_bytes field is set to a value less than size and errno will be changed.
The value of errno may be set to:
See Table 12 and Table 14 for errno settings.
Example that uses _Rreadk()
#include <stdio.h>
#include <recio.h>
#include <stdlib.h>
int main(void)
{
_RFILE *fp;
_RIOFB_T *fb;
char buf[4];
/* Create a physical file */
system("CRTPF FILE(QTEMP/MY_FILE)");
/* Open the file for write */
if ( (fp = _Ropen("QTEMP/MY_FILE", "wr")) == NULL )
{
printf("open for write fails\n");
exit(1);
}
/* write some records into the file */
_Rwrite(fp, "KEY9", 4);
_Rwrite(fp, "KEY8", 4);
_Rwrite(fp, "KEY7", 4);
_Rwrite(fp, "KEY6", 4);
_Rwrite(fp, "KEY5", 4);
_Rwrite(fp, "KEY4", 4);
_Rwrite(fp, "KEY3", 4);
_Rwrite(fp, "KEY2", 4);
_Rwrite(fp, "KEY1", 4);
/* Close the file */
_Rclose(fp);
/* Open the file for read */
if ( (fp = _Ropen("QTEMP/MY_FILE", "rr")) == NULL )
{
printf("open for read fails\n");
exit(2);
}
/* Read the record with key KEY3 */
fb = _Rreadk(fp, buf, 4, __KEY_EQ, "KEY3", 4);
printf("record %d with value %4.4s\n", fb->rrn, buf);
/* Read the next record with key less than KEY3 */
fb = _Rreadk(fp, buf, 4, __KEY_LT, "KEY3", 4);
printf("record %d with value %4.4s\n", fb->rrn, buf);
/* Read the next record with key greater than KEY3 */
fb = _Rreadk(fp, buf, 4, __KEY_GT, "KEY3", 4);
printf("record %d with value %4.4s\n", fb->rrn, buf);
/* Read the next record with different key */
fb = _Rreadk(fp, buf, 4, __KEY_NEXTUNQ, "", 4);
printf("record %d with value %4.4s\n", fb->rrn, buf);
/* Close the file */
_Rclose(fp);
}
Related Information