ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Working with the IFS in RPG IV

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Working with the IFS in RPG IV

    After reading Scott Klements tutorial entitled Working with the IFS in RPG IV. I decided to try a little programming myself. Below is the code. I didn't include the definitions for the flags and mode for the sake of space but I copied and pasted them straight out of Scott's web page, During debugging, put a break point right before the line in red and the value of rddata is always a bunch of, illegible characters, looking nothing like what is in the Vendor.csv file itself. I don't know if the character set id which is 437 is the problem or what. Maybe someone can shed some light.

    Code:
    D open            PR            10I 0 extproc('open')          
    D   path                          *   value options(*string)   
    D   oflag                       10I 0 value                    
    D   mode                        10U 0 value options(*nopass)   
    D   codepage                    10U 0 value options(*nopass)   
     *                                                             
     ** READ api                                                   
    D read            PR            10I 0 extproc('read')          
    D   fildes                      10I 0 value                    
    D   buf                           *   value                    
    D   nbyte                       10U 0 value                    
                                                                   
    D @__errno        PR              *   ExtProc('__errno')       
                                                                   
    D strerror        PR              *   ExtProc('strerror')      
    D    errnum                     10I 0 value                    
                                                                   
     /free                                           
       PATH = '/home/mystuf/Vendor.csv';             
       flags = O_RDWR;                               
       mode =  S_IRUSR +                             
               S_IWUSR +                             
               S_IXUSR ;                             
       fd = open(%trimr(path):flags: mode);          
       if fd < 0;                                    
        Msg = %str(strerror(errno));                 
       endif;                                        
                                                     
                                                     
       len = read(fd: %addr(rddata):%size(rddata));  
       Dow fd >=0;                                   
       [COLOR="#FF0000"]msg = rddata;   [/COLOR]                              
       len = read(fd: %addr(rddata):%size(rddata));  
       enddo;                     
       *INLR = *ON;               
     /end-free

  • #2
    Re: Working with the IFS in RPG IV

    This is a wonderful tutorial and it has helped me a lot in November sorting out our different supplier payment systems which all write out to IFS files in different formats. Now I just have just one program that does all the types.

    @scott... I am starting a new website (www.freeistuff.com) that contains links to free software, IBM i forums, and it will also (coming this weekend) contain links to tutorials, presentations and code examples etc. May I put in links to your site?
    Regards

    Kit
    http://www.ecofitonline.com
    DeskfIT - ChangefIT - XrefIT
    ___________________________________
    There are only 3 kinds of people -
    Those that can count and those that can't.

    Comment


    • #3
      Re: Working with the IFS in RPG IV

      Originally posted by gregwga50
      I don't know if the character set id which is 437 is the problem or what. Maybe someone can shed some light.
      First thing I would change is this line:
      Code:
         Dow fd >=0;
      
      Change to:
         Dow len > 0;
      The fd isn't likely to change, so the loop isn't likely to end.

      As for your question, I suggest that you display maybe the first 40 bytes of rddata in hex when you're in debug. And in a second session, display the first 40 bytes of /home/mystuf/Vendor.csv, also in hex. (You can use the DSPF command to display the file; then use F10 to switch to the hex view.)

      Compare the actual hex values of the file to the hex values from the read() API. Assuming that the rest of your program is correct, you should see an exact match.
      Tom

      There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

      Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

      Comment


      • #4
        Re: Working with the IFS in RPG IV

        BTW, one hint...

        After you review the hex values, try changing this line:
        Code:
           flags = O_RDWR;
        
        Change to:
           flags = O_RDWR +
                   O_TEXTDATA ;
        I assume that O_TEXTDATA is defined in your program as [10i 0] and has the decimal value 16777216.
        Tom

        There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

        Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

        Comment


        • #5
          Re: Working with the IFS in RPG IV

          Originally posted by tomliotta View Post
          BTW, one hint...

          After you review the hex values, try changing this line:
          Code:
             flags = O_RDWR;
          
          Change to:
             flags = O_RDWR +
                     O_TEXTDATA ;
          I assume that O_TEXTDATA is defined in your program as [10i 0] and has the decimal value 16777216.
          I changed flags = O_RDWR to flags = O_RDWR + O_TEXTDATA ; and that solved the problem.

          Comment


          • #6
            Re: Working with the IFS in RPG IV

            A few thoughts:
            • I definitely agree with Tom about the "dow fd >= 0". As far as I can tell, this would create an endless loop -- and is definitely incorrect. If this was copied out of my tutorial, please tell me where so I can fix it, this is a very big bug.
            • The 'mode' parameter is not needed here as you are not specifying O_CREAT. Since no file is created, there's no need to specify what it's authorities will be.
            • I would suggest using O_CCSID in addition to O_TEXTDATA. O_TEXTDATA tells it to treat the data in the file as text, and therefore it will translate from the file's code page (you said this is 437, which I doubt is accurate as 437 is obsolete, but maybe it's close enough) to your job's code page. This will work fine as long as you are only working with code pages, but if you add the O_CCSID flag it'll work with CCSIDs rather than code pages, which will be more versatile.


            So I would suggest changing your open to:
            Code:
               PATH = '/home/mystuf/Vendor.csv';             
               flags = O_RDWR + O_TEXTDATA + O_CCSID;
               fd = open(%trimr(path):flags: 0: 0 );
            The two zeros at the end are needed. The first one is for the mode -- which is ignored here. The second is for the CCSID of your program... 0 is a special value which means to get the CCSID from the job attributes.

            Comment


            • #7
              Re: Working with the IFS in RPG IV

              Originally posted by kitvb1 View Post
              @scott... I am starting a new website (www.freeistuff.com) that contains links to free software, IBM i forums, and it will also (coming this weekend) contain links to tutorials, presentations and code examples etc. May I put in links to your site?
              Yes, please do provide links.

              Please do not, however, make copies of my stuff. Just link to it on my site. (That's what you said you would do, but I wanted to emphasize it.)

              Comment


              • #8
                Re: Working with the IFS in RPG IV

                Scott is definitely right about using the O_CCSID flag. I usually go a little slower than Scott does because I'm never sure how much can be absorbed, especially when the starting point is essentially at the beginning of the concepts. And he seems (almost) always to have all relevant details already in mind when he's explaining a subject.

                But if you go through the steps of seeing how the view of the data changes at each point (and reviewing documentation), it should be pretty clear at the end.

                Tom
                Tom

                There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

                Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

                Comment

                Working...
                X