FileIO Examples

This appendix describes two examples of using FileIO. One example reads data from a database and saves it to a file, and the other example reads data from a file and saves it to a database.

1. Database File Types

figureA 1
Example of Using FileIO of the Database File Type

The following is source code for Example of Using FileIO of the Database File Type.

static long innerModule1(test2Context *context)
{
    long rc = RC_NRM;
    /* File Descriptor */
    FILE *fd1=NULL;

    // User Variables Declaration

    {
        /**************************************
①       * KIND     : DBIO Call
         * NODE ID  : 2
         * NAME     : pfm_emp_p_s999
         * DESCRIPTION :
         *************************************/
        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:DBIO_DYNAMIC_PARAMETER NODEID2------------------//
        // TODO Auto-generated method stub
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:DBIO_DYNAMIC_PARAMETER NODEID2------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:BEFORE_CODE NODEID2------------------//
        // TODO Auto-generated method stub
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:BEFORE_CODE NODEID2------------------//

        /**************************************
         *   KIND              : DBIO Callee Info
         *   NAME              : pfm_emp_p_s999
         *   EXEC              : SELECT
         *   INPUT             : pfm_emp_p_s999In
         *   OUTPUT            : pfm_emp_p_s999Out
         *   ARRAY INPUT       : NONE
         *   ARRAY OUTPUT      : pfm_emp_p_s999BundleOut
         *   DYNAMIC STRUCTURE : NONE
         *************************************/
        PFM_TRY(pfmDbioSelectArray("pfm_emp_p_s999", 3, &context->inCtxpfm_emp_p_s999, context->outCtxpfm_emp_p_s999Ary, NULL, PFMDBIO_NOLOCK));
        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:DBIO_EXCEPTION NODEID2------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:DBIO_EXCEPTION NODEID2------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:AFTER_CODE NODEID2------------------//
        // TODO Auto-generated method stub
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:AFTER_CODE NODEID2------------------//

    }
    {
        /**************************************
         * KIND     : FILEIO Call
         * NODE ID  : 1
         * NAME     : fio TEST01
         * DESCRIPTION :
         *************************************/
②      PFM_TRYNJ(pfmFileIOOpen(&fd1, "/tmp/FileIOData.txt", NULL, FILE_WRITE));
        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:FILEIO_OPEN_EXCEPTION NODEID1------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:FILEIO_OPEN_EXCEPTION NODEID1------------------//

        fioTEST01Data temporaryInput1[3];

        /* File IO Struct Define */
        PfmFileIOStruct pfmFileIOStruct1 = {"fioTEST01Data", 3, "F", 0, NULL, 0, NULL };

        bzero(temporaryInput1, sizeof(temporaryInput1));

        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:BEFORE_CODE NODEID1------------------//

        /*******************************************
③       *       File IO  Input Value Setting       *
         *******************************************/
        pfmFileIOStruct1[0].empno = context->outCtxpfm_emp_p_s999Ary[0]->empno ;
        PFM_STRCPY( pfmFileIOStruct1[0].ename, context->outCtxpfm_emp_p_s999Ary[0]->ename );
        PFM_STRCPY( pfmFileIOStruct1[0].job, context->outCtxpfm_emp_p_s999Ary[0]->job );
        PFM_STRCPY( pfmFileIOStruct1[0].sal, context->outCtxpfm_emp_p_s999Ary[0]->sal );

        pfmFileIOStruct1[1].empno = context->outCtxpfm_emp_p_s999Ary[1]->empno ;
        PFM_STRCPY( pfmFileIOStruct1[1].ename, context->outCtxpfm_emp_p_s999Ary[1]->ename );
        PFM_STRCPY( pfmFileIOStruct1[1].job, context->outCtxpfm_emp_p_s999Ary[1]->job );
        PFM_STRCPY( pfmFileIOStruct1[1].sal, context->outCtxpfm_emp_p_s999Ary[1]->sal );

        pfmFileIOStruct1[2].empno = context->outCtxpfm_emp_p_s999Ary[2]->empno ;
        PFM_STRCPY( pfmFileIOStruct1[2].ename, context->outCtxpfm_emp_p_s999Ary[2]->ename );
        PFM_STRCPY( pfmFileIOStruct1[2].job, context->outCtxpfm_emp_p_s999Ary[2]->job );
        PFM_STRCPY( pfmFileIOStruct1[2].sal, context->outCtxpfm_emp_p_s999Ary[2]->sal );

        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:BEFORE_CODE NODEID1------------------//

        /* Read Buffer Struct */
        pfmFileIOStruct1.structData = &temporaryInput1[0];
        pfmFileIOStruct1.structSize = sizeof(fioTEST01Data);

④      PFM_TRYNJ(pfmFileIOWrite(&fd1, &pfmFileIOStruct1));

        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:FILEIO_EXCEPTION NODEID1------------------//
        if( rc != RC_NRM ) {
            PFM_DBG("NULL File Write Error!");
            PFM_TRY( pfmFileIOClose( &fd1 ) );
            return RC_ERR;
        }
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:FILEIO_EXCEPTION NODEID1------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:AFTER_CODE NODEID1------------------//
        // TODO Auto-generated method stub
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:AFTER_CODE NODEID1------------------//

        PFM_TRYNJ(pfmFileIOClose(&fd1));
        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:FILEIO_CLOSE_EXCEPTION NODEID1------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:FILEIO_CLOSE_EXCEPTION NODEID1------------------//

    }
    return RC_NRM;

PFM_CATCH:
    return rc;
}

The following describes the previous source code.

① Uses the DBIO module to access the database and read data.

image

② Opens the FileIO module to get the file descriptor.

image

③ Sets the input structure of FileIO as an output structure that includes the result value of the DBIO module.

image

④ Saves the FileIO structure data to the file.

image

2. File Database Types

figureA 2
Example of Using FileIO of the File Database Type

The following is source code for Example of Using FileIO of the File Database Type.

static long innerModule1(test2Context *context)
{
    long rc = RC_NRM;
    /* File Descriptor */
    FILE *fd1=NULL;

    // User Variables Declaration

    {
        /**************************************
         * KIND     : FILEIO Call
         * NODE ID  : 1
         * NAME     : fio TEST01
         * DESCRIPTION :
         *************************************/
①      PFM_TRYNJ(pfmFileIOOpen(&fd1, "/tmp/FileIOData.txt", NULL, FILE_READ));
        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:FILEIO_OPEN_EXCEPTION NODEID1------------------//
        if(rc!=RC_NRM) {
           PFM_DBG("[%s] File open Error!!", /tmp/FileIOData.txt);
           return RC_ERR;
         }
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:FILEIO_OPEN_EXCEPTION NODEID1------------------//

        fioTEST01Data temporaryOutput1[3];

       /* File IO Struct Define */
       PfmFileIOStruct pfmFileIOStruct1 = {"fioTEST01Data", 3, "F", 0, NULL, 0, NULL };

       bzero(temporaryOutput1, sizeof(temporaryOutput1));

       //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:BEFORE_CODE NODEID1------------------//

       //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:BEFORE_CODE NODEID1------------------//

       /* Read Buffer Struct */
       pfmFileIOStruct1.structData = &temporaryOutput1[0];
       pfmFileIOStruct1.structSize = sizeof(fioTEST01Data);

       PFM_STRYNJ(pfmFileIORead(&fd1, &pfmFileIOStruct1));

       //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:FILEIO_EXCEPTION NODEID1------------------//
       if( rc != RC_NRM ) {
            PFM_DBG("NULL File Write Error!");
            PFM_TRY( pfmFileIOClose( &fd1 ) );
            return RC_ERR;
       }
       //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:FILEIO_EXCEPTION NODEID1------------------//

       //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:AFTER_CODE NODEID1------------------//

      /*******************************************
       *       File IO  Input Value Setting       *
       *******************************************/
      context->inCtxpfm_emp_p_i999Ary[0].empno = pfmFileIOStruct1[0]->empno ;
      PFM_STRCPY( context->inCtxpfm_emp_p_i999Ary[0].ename, pfmFileIOStruct1[0]->ename );
      PFM_STRCPY( context->inCtxpfm_emp_p_i999Ary[0].sal, pfmFileIOStruct1[0]->sal );

      context->inCtxpfm_emp_p_i999Ary[1].empno = pfmFileIOStruct1[1]->empno ;
      PFM_STRCPY( context->inCtxpfm_emp_p_i999Ary[1].ename, pfmFileIOStruct1[1]->ename );
      PFM_STRCPY( context->inCtxpfm_emp_p_i999Ary[1].sal, pfmFileIOStruct1[1]->sal );

      context->inCtxpfm_emp_p_i999Ary[2].empno = pfmFileIOStruct1[2]->empno ;
      PFM_STRCPY( context->inCtxpfm_emp_p_i999Ary[2].ename, pfmFileIOStruct1[2]->ename );
      PFM_STRCPY( context->inCtxpfm_emp_p_i999Ary[2].sal, pfmFileIOStruct1[2]->sal );

      //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:AFTER_CODE NODEID1------------------//

      PFM_TRYNJ(pfmFileIOClose(&fd1));
      //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:FILEIO_CLOSE_EXCEPTION NODEID1------------------//
      if(rc!=RC_NRM) {
         PFM_DBG("[%s] File close Error!!", /tmp/FileIOData.txt);
         return RC_ERR;
      }
      //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:FILEIO_CLOSE_EXCEPTION NODEID1------------------//

    }
    {
        /**************************************
②       * KIND     : DBIO Call
         * NODE ID  : 2
         * NAME     : pfm_emp_p_i999
         * DESCRIPTION :
         *************************************/
        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:DBIO_DYNAMIC_PARAMETER NODEID2------------------//
        // TODO Auto-generated method stub
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:DBIO_DYNAMIC_PARAMETER NODEID2------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:BEFORE_CODE NODEID2------------------//
        // TODO Auto-generated method stub
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:BEFORE_CODE NODEID2------------------//

        /**************************************
         *   KIND              : DBIO Callee Info
         *   NAME              : pfm_emp_p_i999
         *   EXEC              : INSERT
         *   INPUT             : pfm_emp_p_i999In
         *   OUTPUT            : pfm_emp_p_i999Out
         *   ARRAY INPUT       : pfm_emp_p_i999BundleIn
         *   ARRAY OUTPUT      : NONE
         *   DYNAMIC STRUCTURE : NONE
         *************************************/
        PFM_TRY(pfmDbioDmlArray("pfm_emp_p_i999", 3, context->inCtxpfm_emp_p_i999Ary, NULL, PFMDBIO_NOLOCK));
        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:DBIO_EXCEPTION NODEID2------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:DBIO_EXCEPTION NODEID2------------------//

        //DO_NOT_MODIFY_THIS_LINE-----------START_OF_CODE:AFTER_CODE NODEID2------------------//
        // TODO Auto-generated method stub
        //DO_NOT_MODIFY_THIS_LINE-----------END_OF_CODE:AFTER_CODE NODEID2------------------//

    }
    return RC_NRM;

PFM_CATCH:
    return rc;
}

The following describes the previous source code.

① Opens the FileIO module to get the file descriptor.

image

② Call the DBIO module.

image