View previous topic :: View next topic |
Author |
Message |
kirank Beginner
Joined: 21 Apr 2004 Posts: 61 Topics: 33 Location: hyderabad
|
Posted: Thu Apr 29, 2004 2:11 am Post subject: regarding input procedures in sort |
|
|
Hi,
i've a doubt in the following code:
Code: |
SelectCSISGraduates.
OPEN INPUT GraduateInfoFile
READ GraduateInfoFile
AT END SET EndOfGradFile TO TRUE
END-READ
PERFORM UNTIL EndOfGradFile
IF CSISGraduate
MOVE StudentNameGF TO StudentNameWF
MOVE GradYearGF TO GradYearWF
MOVE CourseCodeGF TO CourseCodeWF
MOVE EmailDomainGF TO EmailDomainWF
MOVE CountryCodeGF TO CountryCodeWF
RELEASE WorkRec
ELSE
DISPLAY "Rejected - " StudentNameGF SPACE CourseCodeGF
END-IF
READ GraduateInfoFile
AT END SET EndOfGradFile TO TRUE
END-READ
END-PERFORM
CLOSE GraduateInfoFile.
|
SelectCSISGraduates is a input procedure name.
my doubt is he is using READ verb two times in the code. But he is closing the file only one time.
But as per my knowledge , if we want open the same file two times, then, the procedure is:
first we open the file
then closethe file then
the again opend the same file
then close the file.
pls , clarify the doubt |
|
Back to top |
|
|
Mike Chantrey Intermediate
Joined: 10 Sep 2003 Posts: 234 Topics: 1 Location: Wansford
|
Posted: Thu Apr 29, 2004 9:27 am Post subject: |
|
|
Just to expand on the 'why two reads' aspect - it is not logically necessary, you can code an equivalent with only one read in the loop as per below, but this is less efficient since you effectively have an test statement inside the loop to prevent processing beyond the end of the file.
Example recoded with one READ:
Code: |
Select CSISGraduates.
OPEN INPUT GraduateInfoFile
SET EndOfGradFile TO FALSE
PERFORM UNTIL EndOfGradFile
READ GraduateInfoFile
NOT AT END
IF CSISGraduate
MOVE StudentNameGF TO StudentNameWF
MOVE GradYearGF TO GradYearWF
MOVE CourseCodeGF TO CourseCodeWF
MOVE EmailDomainGF TO EmailDomainWF
MOVE CountryCodeGF TO CountryCodeWF
RELEASE WorkRec
ELSE
DISPLAY "Rejected - " StudentNameGF SPACE CourseCodeGF
END-IF
AT END
SET EndOfGradFile TO TRUE
END-READ
END-PERFORM
CLOSE GraduateInfoFile.
|
This assumes your COBOL dialgoue supports NOT AT END (as IBM m/f COBOL does for COBOL 2 onwards); if not you can use an IF statement for the same effect.
As per above, it's probably a little less efficient than having an extra 'priming read' (as it's often called) before the loop but if you're doing I/O anyhow the tiny bit of extra CPU will probably not matter. And I think it's a bit more understandable (though I know that's a matter of opinion). |
|
Back to top |
|
|
|
|