Blog Archive

How to Read an IFS file on iSeries(AS400) using RPGLE or C++

What the integrated file system is?

The integrated file system is a part of OS/400 that supports stream input/output and storage management similar to personal computer and UNIX operating systems, while providing an integrating structure over all information stored in your server.

The key features of the integrated file system are the following:

  • Support for storing information in stream files that can contain long continuous strings of data. These strings of data might be, for example, the text of a document or the picture elements in a picture. The stream file support is designed for efficient use in client/server applications.
  • A hierarchical directory structure that allows objects to be organized like fruit on the branches of a tree. Specifying the path through the directories to an object accesses the object.
  • A common interface that allows users and applications to access not only the stream files but also database files, documents, and other objects that are stored in your server.
  • A common view of stream files that are stored locally on your server, Integrated xSeries Server for iSeries, or a remote Windows NT server. Stream files can also be stored remotely on a Local Area Network (LAN) server, a Novell NetWare server, another remote iSeries server, or a Network File System server. 

The integrated file system is comprised of 11 file systems, each with their own set of logical structures and rules for interacting with information in storage.

The integrated file system interface
Click here if you need source for IFSIO_H

Sample RPGLE Program Reading an IFS File

H Option(*SrcStmt : *NoDebugIO) bnddir('QC2LE')                 
h dftactgrp(*no)                                                
D/copy *libl/QRPGLESRC,IFSIO_H                                  
                         
D readline        PR            10I 0                           
D   fd                          10I 0 value                     
D   text                          *   value                     
D   maxlen                      10I 0 value                     
                                                                
D fd              S             10I 0                           
D line            S            100A                             
D Msg             S             50A                             
D Len             S             10I 0                           
                                                                
 /free                                                          
        fd = open('/directory1/directory2/aaa.htm'                                
             : O_RDONLY+O_TEXTDATA+O_CCSID                      
             : S_IRGRP: 37);                                    
 /end-free                                                      
                                                                
c                   if        fd < 0                            
c                   eval      Msg = 'open(): failed for reading' 
c                   dsply                   Msg                  
c                   eval      *inlr = *on                 
c                   return                                
c                   endif                                 
                                                          
c                   dow       readline(fd: %addr(line):   
c                                          %size(line))>=0
c                   eval      Msg = line                  
c     Msg           dsply                                 
c                   enddo                                 
                                                          
c                   callp     close(fd)                   
                                                          
c                   eval      *inlr = *on                 
c                   return                                
                                                          
P readline        B                                       
D readline        PI            10I 0                     
D   fd                          10I 0 value               
D   text                          *   value               
D   maxlen                      10I 0 value               
                                                          
D rdbuf           S           1024A   static              
D rdpos           S             10I 0 static              
D rdlen           S             10I 0 static                          
                                                                      
D p_retstr        S               *                                   
D RetStr          S          32766A   based(p_retstr)                 
D len             S             10I 0                                 
                                                                      
c                   eval      len = 0                                 
c                   eval      p_retstr = text                         
c                   eval      %subst(RetStr:1:MaxLen) = *blanks       
                                                                      
c                   dow       1 = 1                                   
                                                                      
C* Load the buffer                                                    
c                   if        rdpos>=rdlen                            
c                   eval      rdpos = 0                               
c                   eval      rdlen=read(fd:%addr(rdbuf):%size(rdbuf))
                                                                      
c                   if        rdlen < 1                               
c                   return    -1                                      
c                   endif                                             
c                   endif                                             
                                                                      
C* Is this the end of the line?                                       
c                   eval      rdpos = rdpos + 1             
c                   if        %subst(rdbuf:rdpos:1) = x'25' 
c                   return    len                           
c                   endif                                   
                                                            
C* Otherwise, add it to the text string.                    
c                   if        %subst(rdbuf:rdpos:1) <> x'0d'
c                               and len<>maxlen             
c                   eval      len = len + 1                 
c                   eval      %subst(retstr:len:1) =        
c                               %subst(rdbuf:rdpos:1)       
c                   endif                                   
                                                            
c                   enddo                                   
c                   return    len                           
P                 E                                 

Sample C++ Program Reading an IFS File

  • Source Member type = CPP
#include <iostream>                    
#include <iomanip>                     
#include <fstream>                     
using namespace std;                   
                                       
int main() {                           
    int sum = 0;                       
    char str[250];                     
    int x;                             
    ifstream inFile;                   
                                       
    inFile.open("/directory1/directory2/abc.txt");       
    if (!inFile) {                     
        cout << "Unable to open file"; 
        exit(1); // terminate with error
    }                                  
                                       
    while(inFile) {                    
     inFile.getline(str, 255);         
     cout << str << endl;              
    }                                  
    inFile.close();                    
    return 0;                           
}

5 comments :

  1. Hi,
    Just tried above example. Seems to work with lines max 1024 chars only?
    Replaced 1024 values with 32766 (read xmlfile without lineseparators = 1 string) but it doesn't work.
    Var line seems to be max 1024 chars.

    Any suggestions?

    ReplyDelete
    Replies
    1. Not sure, I have to give it a try but this program is for reading simple files that are line by line basically ending with line feed(CRLF). If you want to read huge XML files just use the parser. Here is an example

      RPGLE XML Parser - XML-SAX and XML-INTO sample code

      Delete
    2. My app is an-in-between-application for executing about 20 different Webservices, to be called by different ERP-apps on our systen. The XML file is generated as a result after calling a webservice, that's why the stream does not include CRLF's.

      Before sending the fil (or data) to the calling program for execution, i want to analyse the file on certain information (error handling). This avoids this errorhandling to be done by each application, code once use anywhere approach.

      so, parse each different webservice result in this application would be a hell of a job...

      Delete
    3. Hi,
      Changed my app to parse the XML file for errorchecking.

      grtz,.

      Delete