Page 1 of 1

read a sequential file till end

PostPosted: Tue Oct 27, 2015 5:08 pm
by arya_starc
OPEN INPUT TESTFILE                                         
DISPLAY 'HERE'                                               
IF  NOT WS-FS = '00'                                         
    DISPLAY 'ERROR OPENING FILE'                             
END-IF                                                       
PERFORM UNTIL WS-EOF = 'Y'                                                                       
   READ TESTFILE                                             
      AT END MOVE 'Y' TO WS-EOF                             
      DISPLAY 'INSIDE READ'                                 
      MOVE ACCOUNT(7:10) TO WS-CUST-KEY                     
      DISPLAY ACCOUNT                                       
 INSPECT WS-CUST-KEY CONVERTING '0123456789' TO 'ABCDEFGHIJ'
 DISPLAY 'WS-CUST-KEY..' WS-CUST-KEY                         
   END-READ                                                 
END-PERFORM                                                 
CLOSE TESTFILE                                               




I need to read the sequential file till end
but as per my code it only display the last record of the sequential file instead of all records.

Re: read a sequential file till end

PostPosted: Tue Oct 27, 2015 6:03 pm
by Robert Sample
When you use READ ... AT END, what is the scope of the AT END statement? Perhaps you should read the Enterprise COBOL Language Reference manual and find out about the END-READ scope terminator? Instead of using AT END, an IF test on the file status (10 means end of input but you can use an 88 level for that) allows you to understand the logic easier.

Re: read a sequential file till end

PostPosted: Wed Oct 28, 2015 10:06 pm
by tivrfoa
Use NOT AT END

READ TESTFILE                                             
   AT END MOVE 'Y' TO WS-EOF                             
   NOT AT END
      DISPLAY 'INSIDE READ'                                 
      MOVE ACCOUNT(7:10) TO WS-CUST-KEY                     
      DISPLAY ACCOUNT                                       
      INSPECT WS-CUST-KEY CONVERTING '0123456789' TO 'ABCDEFGHIJ'
      DISPLAY 'WS-CUST-KEY..' WS-CUST-KEY                         
END-READ

Re: read a sequential file till end

PostPosted: Wed Oct 28, 2015 11:06 pm
by BillyBoyo
Yes, the AT END/NOT AT END tangle is one way to do it. The much better was is as Robert has already suggested.

Re: read a sequential file till end

PostPosted: Wed Oct 28, 2015 11:13 pm
by Terry Heinze
The reason Robert suggested using FILE-STATUS instead of AT END/NOT AT END is that AT END means FILE-STATUS 10. NOT AT END means not only a good read but also all the error conditions as well.

Re: read a sequential file till end

PostPosted: Wed Oct 28, 2015 11:26 pm
by tivrfoa
Terry Heinze wrote:The reason Robert suggested using FILE-STATUS instead of AT END/NOT AT END is that AT END means FILE-STATUS 10. NOT AT END means not only a good read but also all the error conditions as well.

Interesting.
So the correct way would be to check if file-status equals 0 for more records, or 10 for end of file.

http://ibmmainframes.com/references/a27.html
http://www-01.ibm.com/support/knowledge ... fio13e.htm

(stuff relating to something completely different deleted)

Re: read a sequential file till end

PostPosted: Wed Oct 28, 2015 11:35 pm
by Terry Heinze
Many of my programs contain the following paragraph:
      * .-------------------------------------------------------------.
      * |  CALLED FROM:  ALL PARAGRAPHS THAT PERFORM I/O              |
      * |  FUNCTION:  CHECK THE FILE STATUS CODE. IF OKAY OR AT END,  |
      * |               LET THE CALLER DETERMINE THE NEXT ACTION. IF  |
      * |               ANYTHING ELSE, ABEND.                         |
      * .-------------------------------------------------------------.
       X90-CHECK-FILE-STATUS.
           EVALUATE TRUE
               WHEN WS-FS-OK
               WHEN WS-FS-AT-END
                   CONTINUE
               WHEN OTHER
                   PERFORM Z60-FILE-ABEND  THRU Z60-X
           END-EVALUATE
           .
       X90-X.
           EXIT
           .