ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Read IFS file in CSSID 1252 Using open() and read()

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

  • Read IFS file in CSSID 1252 Using open() and read()

    If I already have an IFS file on the IFS in CCSID 1252 (meaning my RPG program did not create it), what is the proper open() and read() parameters to get ascii data into RPG variables? I cannot seem to get passed the open statement. The file already existing on the IFS is a requirement that I am not wanting to get around. I just want to open a pre-existing IFS file and read it. :-)

    Here's my code:

    Code:
    d fd              s             10i 0 inz 
    d ifsError        s               n   inz 
    d len             s             10u 0 inz 
    d flags           s             10u 0 inz 
                                              
    d path            s            256a   inz 
    d data            s           1000a   inz 
    
    d*                                            Reading Only    
    d O_RDONLY        c                   1                       
    
      *--------------------------------------------------------------------------------------------- 
      * Procedures                                                                                   
      *--------------------------------------------------------------------------------------------- 
     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)                                    
                                                                                                     
     d read            PR            10i 0 extproc('read')                                           
     d  fildes                       10i 0 value                                                     
     d  buf                            *   value                                                     
     d  nbyte                        10i 0 value                                                     
                                                                                                     
     d close           pr            10i 0 extproc('close')                                          
     d  fildes                       10i 0 value                                                     
    
     *---------------------------------------------------------------------------------------------
     * Main Program                                                                                
     *---------------------------------------------------------------------------------------------
     /free                                                                                         
                                                                                                   
      // global monitor                                                                            
      monitor;                                                                                     
                                                                                                   
        path = '/a-file-import.csv';                                                               
        flags =  O_RDONLY;                                                                         
        fd = open( path : flags );                                                                 
                                                                                                   
        if fd < 0;                                                                                 
          ifsError = *on;                                                                          
          *inlr = *on;                                                                             
          return;                                                                                  
        endif;                                                                                     
                                                                                                   
        len = read( fd : %addr(data) : %size(data) );                                              
    
        callp close( fd );      
                                                                       
      // global error catch      
      on-error;                  
                                 
      endmon;                    
                                 
      // end program             
      *inlr = *on;               
      return;                    
                                 
     /end-free

  • #2
    Re: Read IFS file in CSSID 1252 Using open() and read()

    The trailing blanks in your path variable are being considered as part of the name by the open() function.

    There's a few ways to avoid passing the blanks to the API. You could add the VARYING keyword on the D spec for "path", or you could use %trim(path) when you call open(), or you could change OPTIONS(*STRING) to OPTIONS(*STRING:*TRIM) on the prototype for the open() function.

    Comment


    • #3
      Re: Read IFS file in CSSID 1252 Using open() and read()

      In addition to what Barbara said, you may also want to add O_TEXTDATA to your flags so that the data is converted from 1252 to EBCDIC when the the file is read. (Unless you prefer that not to happen?)

      Comment


      • #4
        Re: Read IFS file in CSSID 1252 Using open() and read()

        Thanks Barbara & Scott. That worked! I can now see readable values in my data field.

        I changed path to be varying length, and added O_TEXTONLY to my flags.

        Sorry for a such a delayed response. CRLF parsing coming up. :-)

        Comment

        Working...
        X