IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Please explain me this two READ for the same file


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sh_moni
Currently Banned

New User


Joined: 30 Apr 2007
Posts: 7
Location: pune

PostPosted: Mon Jun 25, 2007 7:39 pm
Reply with quote

Code:

READ BOOKSALESFILE                   
    AT END SET ENDOFBSF TO TRUE       
END-READ                             
PERFORM UNTIL ENDOFBSF               
   IF NORMALSALE                     
    RELEASE WORKREC FROM BSF-RECORD   
   END-IF                             
   READ BOOKSALESFILE                 
   AT END SET ENDOFBSF TO TRUE       
   END-READ                           
END-PERFORM                   

Could somebody please explain me this part of code,why we are using here two READ for the same file,if i remove the first one the output file is empty one and if i remove the internal read the program goes in infinite loop.
Please help me out.
Back to top
View user's profile Send private message
TG Murphy

Active User


Joined: 23 Mar 2007
Posts: 148
Location: Ottawa Canada

PostPosted: Mon Jun 25, 2007 8:36 pm
Reply with quote

sh_moni,

It is very common to see 2 READS as shown in your email. In fact that is the way I code it myself. Reason why is that the PERFORM UNTIL is actually a DOWHILE construct - not a DOUNTIL as the COBOL syntax would suggest. The condition gets tested first and then the logic within the loop only executes if the condition is true.

The alternative coding that would let you code just a single READ would look something like this:

PERFORM WITH TEST AFTER UNTIL EOF
PERFORM READ
IF NOT EOF
PERFORM PROCESS-RECORD
END-IF
END-PERFORM

And this is also a perfectly fine way to do it.

However, there are few common types of programs where you need special logic that executes ONLY for the first record that you read. (ie control break logic) For these programs, the "2 READ" approach is perfectly suited to doing this. So there is a good reason why you bump into the "2 READ" approach so often.
Back to top
View user's profile Send private message
ramfrom84

New User


Joined: 23 Aug 2006
Posts: 93
Location: chennai

PostPosted: Mon Jun 25, 2007 8:37 pm
Reply with quote

Hi,
Mostly READ can be used for TWO times , only if we want the prevoius record value for comparison or rolling up the records , then they will read one time at the begining , for next time onwards u can read and do the process with operation(having previous value).

It is suggestion since u have mention only few code, u can trace the program for better understanding.
Back to top
View user's profile Send private message
socker_dad

Active User


Joined: 05 Dec 2006
Posts: 177
Location: Seattle, WA

PostPosted: Tue Jun 26, 2007 1:46 am
Reply with quote

To put it in ancient terminology (cause I am an old fart and read and write this kinf od code all day!), this is what your code does.

The first read is called an initial read. It reads the file and loads the first record for subsequent processing. If there is no first record, the end of file condition is set, in your case, ENDOFBSF. The END-READ closes this statement.

The PERFORM UNTIL is a separate piece of logic. It basically states that we are going to test the NORMALSALE condition and Release the Workrec from the BSF-RECORD if it is true. Then we read the next record in the Book Sale File, setting ENDOFBSF to true when we reach the end of the file.

Once the condition ENDOFBSF is true, we drop out of the PERFORM UNTIL statement. This could happen on the first record or on the ninety-first record.

The idea is that you use the first Read once to open the file and get the first record, perform processing on the record, then read the next record. The UNTIL logic keeps your program from attempting to perform operations on records that do not exist. And the Read at the bottom of the Perform logic allows you to read the next record and set an end of file condition before attempting to do any work on the record.

As previously mentioned, this is very, very common logic and if you work a lot with batch programs, you will find many of them use this basic logic.

Hope that was clear.
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3156
Location: Tucson AZ

PostPosted: Tue Jun 26, 2007 3:43 am
Reply with quote

To add to Dad's post.....

When you program, there are three parts of the logic. The main and primary is the central logic - the read/write sort of thing. Before and after that are the "boundary conditions", how to start and how to finish....the initial read set up the program so that it could loop through the central section seamlessly.

Personally, when I code such logic, I always start with the loop and then figure out how to start and stop it....

Good luck.
Back to top
View user's profile Send private message
sh_moni
Currently Banned

New User


Joined: 30 Apr 2007
Posts: 7
Location: pune

PostPosted: Tue Jun 26, 2007 2:00 pm
Reply with quote

hi all,

many thanks for your explanations,its quite clear to me know.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 3
No new posts FTP VB File from Mainframe retaining ... JCL & VSAM 8
No new posts Extract the file name from another fi... DFSORT/ICETOOL 6
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Error to read log with rexx CLIST & REXX 11
Search our Forums:

Back to Top