read a sequential file till end



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

read a sequential file till end

Postby arya_starc » Tue Oct 27, 2015 5:08 pm

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.
arya_starc
 
Posts: 136
Joined: Mon Sep 21, 2015 1:39 pm
Has thanked: 5 times
Been thanked: 0 time

Re: read a sequential file till end

Postby Robert Sample » Tue Oct 27, 2015 6:03 pm

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.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: read a sequential file till end

Postby tivrfoa » Wed Oct 28, 2015 10:06 pm

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
tivrfoa
 
Posts: 84
Joined: Wed Aug 22, 2012 6:35 pm
Has thanked: 60 times
Been thanked: 0 time

Re: read a sequential file till end

Postby BillyBoyo » Wed Oct 28, 2015 11:06 pm

Yes, the AT END/NOT AT END tangle is one way to do it. The much better was is as Robert has already suggested.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: read a sequential file till end

Postby Terry Heinze » Wed Oct 28, 2015 11:13 pm

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.
.... Terry

These users thanked the author Terry Heinze for the post:
tivrfoa (Wed Oct 28, 2015 11:22 pm)
Terry Heinze
 
Posts: 239
Joined: Wed Dec 04, 2013 11:08 pm
Location: Richfield, MN, USA
Has thanked: 12 times
Been thanked: 11 times

Re: read a sequential file till end

Postby tivrfoa » Wed Oct 28, 2015 11:26 pm

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)
tivrfoa
 
Posts: 84
Joined: Wed Aug 22, 2012 6:35 pm
Has thanked: 60 times
Been thanked: 0 time

Re: read a sequential file till end

Postby Terry Heinze » Wed Oct 28, 2015 11:35 pm

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
           .
.... Terry

These users thanked the author Terry Heinze for the post:
tivrfoa (Wed Oct 28, 2015 11:39 pm)
Terry Heinze
 
Posts: 239
Joined: Wed Dec 04, 2013 11:08 pm
Location: Richfield, MN, USA
Has thanked: 12 times
Been thanked: 11 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post