使用记录输入和输出函数处理数据库记录文件

您可以从数据文件中读取和打印记录。

示例:

以下示例使用 _Ropen()_Rreadl()_Rreadp()_Rreads()_Rreadd()_Rreadf()_Rrlslck()_Rdelete()_Ropnfbk()_Rclose() 记录 I/O 函数。 程序 T1520REC 从数据文件 T1520DD4中读取和打印记录。
  1. 在命令行上,输入:
    CRTPF FILE(MYLIB/T1520DD4) SRCFILE(QCPPLE/QADDSSRC)
    这将创建使用以下 DDS 的物理文件 T1520DD4 :
    图 1。 T1520DD4 -数据库记录的 DDS 源
         A          R PURCHASE
         A            ITEMNAME      10
         A            SERIALNUM     10
         A          K SERIALNUM
  2. 在 T1520DD4:
    orange    1000222200
    grape     1000222010
    apple     1000222030
    cherry    1000222020
  3. 在命令行上,输入:
    CRTBNDC PGM(MYLIB/T1520REC) SRCFILE(QCPPLE/QACSRC).
    这将创建使用以下源的程序 T1520REC :
    图 2。 T1520REC -ILE C 使用记录 I/O 函数处理数据库文件的源
    /* This program illustrates how to use the _Rreadp(), _Rreads(),      */
    /* _Rreadd(), _Rreadf(), _Rreadn(),
    /* _Ropnfbk(), _Rdelete, and _Rrlslck() functions.                    */
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <recio.h>
     
    int main(void)
    {
        char        buf[21];
        _RFILE      *fp;
        _XXOPFB_T    *opfb;
    /* Open the file for processing in arrival sequence.                  */
     
        if (( fp = _Ropen ( "*LIBL/T1520DD4", "rr+, arrseq=Y" )) == NULL )
        {
            printf ( "Open failed\n" );
            exit ( 1 );
        };
    /* Get the library and file names of the file opened.                 */
        opfb = _Ropnfbk ( fp );
        printf ( "Library: %10.10s\nFile:    %10.10s\n",
                  opfb->library_name,
                  opfb->file_name);
    /* Get the last record.                                               */
        _Rreadl ( fp, NULL, 20, __DFT );
        printf ( "Fourth record: %10.10s\n", *(fp->in_buf) ); 
    /* Get the third record.                                              */
        _Rreadp ( fp, NULL, 20, __DFT );
        printf ( "Third record:  %10.10s\n", *(fp->in_buf) );
    /* Release lock on the record so another function can access it.      */
        _Rrlslck ( fp );
    /* Read the same record.                                              */
        _Rreads ( fp, NULL, 20, __DFT );
        printf ( "Same record:  %10.10s\n", *(fp->in_buf) );
    /* Get the second record without locking it.                          */
        _Rreadd ( fp, NULL, 20, __NO_LOCK, 2 );
        printf ( "Second record: %10.10s\n", *(fp->in_buf) );
    /* Get the first record.                                              */
        _Rreadf ( fp, NULL, 20, __DFT );
        printf ( "First record:  %10.10s\n", *(fp->in_buf) );
    /* Delete the second record.                                          */
        _Rreadn ( fp, NULL, 20, __DFT );
        _Rdelete ( fp );
    /* Read all records after deletion.                                   */
        _Rreadf ( fp, NULL, 20, __DFT );
        printf ( "First record after deletion: %10.10s\n", *(fp->in_buf));
        _Rreadn ( fp, NULL, 20, __DFT );
        printf ( "Second record after deletion: %10.10s\n", *(fp->in_buf));
        _Rreadn ( fp, NULL, 20, __DFT );
        printf ( "Third record after deletion:  %10.10s\n", *(fp->in_buf));
      
        _Rclose ( fp );
    }
    

    _Ropen() 函数将打开文件 T1520DD4。 _Ropnfbk() 函数获取库名 MYLIB 和文件名 T1520DD4。 _Rreadl() 函数读取第四条记录 "cherry 1000222020"。 _Rreadp() 函数读取第三条记录 " apple 1000222030 "。 _Rrlslck() 函数释放对此记录的锁定,以便 _Rreads() 可以再次读取该记录。 _Rreadd() 函数读取第二条记录 "葡萄 1000222010" 而不将其锁定。 _Rreadf() 函数读取第一条记录 "橙色 1000222200"。 _Rdelete() 函数将删除第二个记录。 然后读取并打印所有记录。

  4. 运行程序 T1520REC。 在命令行上,输入:
    CALL PGM(MYLIB/T1520REC)
    屏幕输出如下所示:
      Library: MYLIB
      File:    T1520DD4
      Fourth record: cherry
      Third record:  apple
      Same record:  apple
      Second record: grape
      First record:  orange
      First record after deletion: orange
      Second record after deletion: apple
      Third record after deletion:  cherry
      Press ENTER to end terminal session.
    物理文件 T1520DD4 包含以下数据:
    ORANGE    1000222200
    APPLE     1000222030
    CHERRY    1000222020