ILE C/C++ Programmer's Guide

Record Functions for Diskette Files

Use the following record functions to process diskette files:

Example:

The following example shows how to write to a diskette file.

  1. To create the diskette file T1520DKF, enter:

    CRTDKTF FILE(MYLIB/T1520DKF) DEV(DKT02) LABEL(FILE1)
    EXCHTYPE(*I) SPOOL(*NO)
  2. Enter:

    CRTPF FILE(MYLIB/T1520DDI) SRCFILE(QCPPLE/QADDSSRC)
    SHARE(*YES)

    To create the physical file T1520DDI using the following DDS source:



    
    
    A R CUST
    A NAME 20A
    A AGE 3B
    A DENTAL 6B
  3. Type the following records into the database file T1520DDI:



    
    
    Dave Bolt 35 350
    Mary Smith 54 444
    Mike Tomas 25 545
    Alex Michaels 32 512
  4. To select only records that have a value greater than 400 in the DENTAL field, enter:

    OPNQRYF FILE((MYLIB/T1520DDI)) QRYSLT('DENTAL *GT 400') OPNSCOPE(*JOB)
  5. To create the program T1520DSK using the source shown below, enter:

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

    Figure 157. T1520DSK -- ILE C Source to Write Records to a Diskette File


    
    
    /* This program illustrates how to write to a diskette file. */
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <recio.h>
     
    #define BUF_SIZE 30
     
    int main(void)
    {
    _RFILE *dktf;
    _RFILE *dbf;
    char buf [BUF_SIZE];
     
    /* Open the diskette file */
     
    if (( dktf = _Ropen ( "*LIBL/T1520DKF", "wr blkrcd=y lrecl=100" )) == NULL )
    {
    printf ( "DISKETTE file did not open \n" );
    exit ( 1 );
    }
    
    
    /* Open the database file. */
     
    if ( ( dbf = _Ropen ( "*LIBL/T1520DDI", "rr") ) == NULL)
    {
    printf ( "DATABASE file did not open\n" );
    exit ( 2 );
    }
     
    /* Copy all the database records meeting the OPNQRYF selection */
    /* criteria to the diskette file. */
     
    while (( _Rreadn ( dbf, buf, BUF_SIZE,__DFT) ) -> num_bytes != EOF )
    {
    _Rwrite ( dktf, buf, BUF_SIZE );
    }
    _Rclose ( dktf );
    _Rclose ( dbf );
    }

    The _Ropen() function opens the diskette file T1520DKF and the database file T1520DDI. The _Rreadn() function reads all database records. The _Rwrite() function copies all database records that have a value > 400 in the DENTAL field to the diskette file T1520DKF.

  6. To run the program T1520DSK, enter:

    CALL PGM(MYLIB/T1520DSK)

    The output to the diskette file is as follows:



    
    
    Mary Smith 444
    Mike Tomas 545
    Alex Michaels 512

    After you run the program, the diskette file contains only the records that satisfied the selection criteria.


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