• Subject: Re: UNIX-type API to read stream file until new-line character reached
  • From: "Simon Coulter" <shc@xxxxxxxxxxxxxxxxx>
  • Date: Thu, 12 Oct 00 20:09:45 +1000
  • Importance: Normal

Hello Patrick,

You wrote:
>I've been using the UNIX-type APIs to read 
>stream files but I would like to read from a stream 
>file until a new-line character is reached 
>like the fgets() function in C. Does anyone 
>know if this is possible using the UNIX-type
>APIs? If so how? I'm programming in ILE COBOL v4r4m0.

You can use the C runtime functions from COBOL just as you can with RPG (except 
don't need to prototype them).

I don't consider myself an expert COBOL coder but a couple of hours with the 
manual and 
the COBOL compiler resulted in the following example:

       ID DIVISION.                                                     
         PROGRAM-ID. EXAMPLE.                                           
       WORKING-STORAGE SECTION.                                         
         77 ERRNO-PTR POINTER VALUE NULL.                               
         77 RC PIC S9(9) BINARY.                                        
         77 FILE-NAME PIC X(30).                                        
         77 FILE-MODE PIC X(10).                                        
         77 FILE-PTR POINTER VALUE NULL.                                
         01 BUFFER PIC X(1024).                                         
       LINKAGE SECTION.                                                 
         77 ERRNO PIC S9(9) BINARY.                                     
       PROCEDURE DIVISION.                                              
            STRING "/TEST"  DELIMITED BY SIZE                           
                   X"00" DELIMITED BY SIZE                              
                   INTO FILE-NAME                                       
            STRING "r"  DELIMITED BY SIZE                               
                   X"00" DELIMITED BY SIZE                              
                   INTO FILE-MODE                                       
            CALL PROCEDURE "_C_IFS_fopen"                               
                           USING FILE-NAME,                             
                RETURNING FILE-PTR.                                     
            IF FILE-PTR = NULL                                    
                CALL PROCEDURE "__errno"                          
                  RETURNING ERRNO-PTR                             
               SET ADDRESS OF ERRNO TO ERRNO-PTR                  
               DISPLAY "File open failed. Errno: " ERRNO          
               EXIT PROGRAM                                       
            CALL PROCEDURE "_C_IFS_fgets"                         
                           USING BY VALUE ADDRESS OF BUFFER,      
                                 BY VALUE LENGTH OF BUFFER,       
                                 BY VALUE FILE-PTR.               
            DISPLAY "Data read from file:" BUFFER.
            CALL PROCEDURE "_C_IFS_fclose"                        
                           USING BY VALUE FILE-PTR                
                RETURNING RC.                                     
            EXIT PROGRAM.                                         

When you compile this you must specify OPTIONS(*NOMONOPRC *NOSTDTRUNC).  The 
first value 
stops Cobblers from mono-casing procedure names (needed since the c functions 
generally lower-case) and the second value causes Cobblers to create real 

I am assuming that the file you wish to process is in the IFS.  If it is really 
physical file then remove the _C_IFS_ prefix from the function calls.  

The file /TEST was created using EDTF and simply contains the single line:
        Here is some data to
est the COBOL program.

In fact the hardest part of making this work was getting the stream I/O 
functions to 
process the IFS rather than looking for a PF.  The C compiler hides this by 
using the 
compiler value SYSIFCOPT(*IFSIO) which just defines some magic values to cause 
normal C functions to map the correct IFS functions.  An exercise in following 
includes results in the correct names being found.  Of course, this could be 
hidden much 
more elegantly in RPG but I think you could accomplish the same with procedure 

Simon Coulter.

 FlyByNight Software         AS/400 Technical Specialists       
 Eclipse the competition - run your business on an IBM AS/400.  
 Phone: +61 3 9419 0175      Mobile: +61 0411 091 400           
 Fax:   +61 3 9419 0175      mailto: shc@flybynight.com.au      
 Windoze should not be open at Warp speed.                      

| This is the COBOL/400 Mailing List!
| To submit a new message, send your mail to COBOL400-L@midrange.com.
| To subscribe to this list send email to COBOL400-L-SUB@midrange.com.
| To unsubscribe from this list send email to COBOL400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com

This thread ...

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2020 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].