Wednesday, September 21, 2016

Working with IFS in RPGLE

Read IFS file in RPGLE

Working with IFS file has always been one of the important things while programming with RPGLE.

The most common way to deal with IFS file is 
  • Copy the IFS file to Flat file using CPYFRMIMPF/CPYFRMSTMF.
  • Read the Flat file in RPGLE.
Or 
  • Write the Data into File in RPGLE.
  • Copy the File to IFS by using CPYTOIMPF/CPYTOSTMF.
This involves writing additional CL Program or using QCMDEXC API to execute CL Command to copy the data from/to IFS and Creation of Temporary/Permanent Database file. 

Accessing IFS files has become easy by using 'open', 'close', 'read' and 'write' functions.

Below is an Simple Example on how to Read data from IFS and write into Database file.

**Free
     Ctl-Opt DftActGrp(*No) ;

     // ProtoType Declaration for 'open'
     Dcl-PR OpenIfs Int(10) ExtProc('open') ;
       pIfsPath Pointer Value Options(*String) ;
       pFileStatusFlag Int(10) Value ;
       pFilePermission Uns(10) Value Options(*NoPass) ;
       pOutputCcsId Uns(10) Value Options(*NoPass) ;
     End-PR ;

     // ProtoType Declaration for 'read'
     Dcl-PR ReadIfs Int(10) ExtProc('read') ;
       pFileDescriptor Int(10) Value ;
       pIfsData Pointer Value ;
       pBytesRetrieved Uns(10) Value ;
     End-PR ;

     // ProtoType Declaration for 'close'
     Dcl-PR CloseIfs Int(10) ExtProc('close') ;
       pFileDescriptor Int(10) Value ;
     End-PR ;

     // Constants Declaration
     Dcl-C ReadOnly 1 ;
     Dcl-C Text 16777216 ;
     Dcl-C CcsId 32 ;
     Dcl-C GroupAuthority 32 ;
     Dcl-C Path '/home/PREDDY/SampleIfs.txt' ;

     // Stand Alone Variables Declaration
     Dcl-S FileDescriptor Int(10) ;
     Dcl-S Length Int(10) ;
     Dcl-S IfsData Char(100) ;
     Dcl-S IfsRecord Char(100) ;
     Dcl-S StartPos Packed(3) ;
     Dcl-S EndPos Like(StartPos) ;

     // Open IFS File
     FileDescriptor = OpenIfs(Path
                             :ReadOnly + Text + CcsId
                             :GroupAuthority
                             :37) ;

     If FileDescriptor < 0 ;
       Dsply ('Problem While accessing IFS file.') ;
       *InLr = *On ;
       Return ;
     Else ;
       Dow 1 = 1 ;

         // Read IFS File
         Length = ReadIfs(FileDescriptor : %Addr(IfsData) : %Size(IfsData) ) ;
         If Length = 0 ;
           Leave ;
         ElseIf Length < %Size(IfsData) ;
           %SubSt(IfsData : (Length + 1)) = ' ' ;
         EndIf ;

         StartPos = *Zeros ;

         Dow 2 = 2 ;

           // Write Data into IFS file
           EndPos = %Scan(x'25' : IfsData : StartPos + 1) ;
           If EndPos > *Zeros ;
             IfsRecord = %SubSt(IfsData : StartPos + 1 :
                          EndPos - StartPos - 1) ;
             Dsply IfsRecord ;
             StartPos = EndPos ;
           Else ;
             Leave ;
           EndIf ;
         EndDo ;
       EndDo ;
     EndIf ;

     // Close File
     CloseIfs(FileDescriptor) ;
     *InLr = *On ;

This example has been written in Full Free format RPGLE. Declarations has to be modified to Fixed Format, if any restrictions in writing Full Free Format.

Refer to Scott Klement's ebook for detailed info on IFS in RPG.

No comments:

Post a Comment

Different Ways of Sorting Data in a List - Python

Sorting Data in a List List is a collection of data (of different data types), much like an array. Like any data structure or data set, dat...