ILE C/C++ Programmer's Guide


Processing a Database Record File in Keyed Sequence

You can update a record file by using a keyed sequence access path. The records are arranged based on the contents of one or more key fields in the record.

Example:

The following example updates data in the record file T1520DD3 by using the key field SERIALNUM. The _Rupdate() function is used.

  1. On the command line, enter:

    CRTPF FILE(MYLIB/T1520DD3) SRCFILE(QCPPLE/QADDSSRC)

    To create the physical file T1520DD3 that uses the following DDS source:

    Figure 129. T1520DD3 -- DDS Source for Database Records


    
    
    A R PURCHASE
    A ITEMNAME 10
    A SERIALNUM 10
    A K SERIALNUM
  2. Enter the following sample data into T1520DD3:

    orange 1000222200
    grape 1000222010
    apple 1000222030
    cherry 1000222020

    Although you enter the data as shown, the file T1520DD3 is accessed by the program T1520KSP in keyed sequence. Therefore the program T1520KSP reads the file T1520DD3 in the following sequence:


    grape 1000222010
    cherry 1000222020
    apple 1000222030
    orange 1000222200
  3. On the command line, enter:

    CRTBNDC PGM(MYLIB/T1520KSP) SRCFILE(QCPPLE/QACSRC)

    This creates the program T1520KSP, using the following source:

    Figure 130. T1520KSP -- ILE C Source to Process a Database Record File in Keyed Sequence


    
    
    /* This program illustrates how to update a record in a file using */
    /* the _Rupdate() function. */
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <recio.h>
     
    int main(void)
    {
    _RFILE *in;
    char new_purchase[21] = "PEAR 1002022244";
     
    /* Open the file for processing in keyed sequence. File is created */
    /* with the default access path. */
     
    if ( (in = _Ropen("*LIBL/T1520DD3", "rr+")) == NULL )
    {
    printf("Open failed\n");
    exit(1);
    };
    /* Update the first record in the keyed sequence. The function */
    /* _Rlocate locks the record. */
     
    _Rlocate(in, NULL, 0, __FIRST);
    _Rupdate(in, new_purchase, 20);
    /* Force the end of data. */
     
    _Rfeod(in);
     
    _Rclose(in);

    This program uses the _Ropen() function to open the record file T1520DD3. The default access path which is the keyed sequence access path is used to create the file T1520DD3. The _Rlocate() function locks the first record in the keyed sequence. The _Rupdate() function updates the record that is locked by _Rlocate() to PEAR 1002022244. (The first record becomes the second record in the keyed sequence access path because the key has changed.)

  4. To run the program T1520KSP, enter:

    CALL PGM(MYLIB/T1520KSP)

    because grape is the first record in the keyed sequence, it is updated, and the data file T1520DD3 is as follows:


    orange 1000222200
    PEAR 1002022244
    apple 1000222030
    cherry 1000222020


[ Top of Page | Previous Page | Next Page | Table of Contents ]