Another Scenario mentioned below.
I have one output report file.
First 3 lines are headings
4th line is as ?Status: ------------------? (Possible values are ?Success, Failure and Success with Exception)
From 5th line onwards populated with data.
The 4th line status value will be populated once all the data starting from the 5th line will be processed. If all are processed successfully, the status is ?success?, if its failed then value is ?failure? and if some warning, then the status value is ?success with exception?
Since we write each record sequentially, the 4th record populates first then other data records, but the value of the 4th record depends upon all of the subsequent records. The file should not open twice in the Cobol program.
While you process the data write the processed data into a temporary data file. Once the process is over print the 'Status' in report. Then print the details from the temporary data file you have created.
Create the report as 3 files: file1 contains the 1st 3 lines, file2 contains the 5th thru EOF, the 3rd contains line 4 and ic created after all other lines in the report.
The next step (IEBGENR or SORT or some such) copies the 3 files in 1-3-2 concatenation order to SYSOUT.
You could also print the recs from an internal table, but that can be problematic with larger reports.