Page 1 of 1

Writing a file in sub program

PostPosted: Tue Apr 14, 2015 1:12 pm
by sachu1912
I have the below requirement.

I need to compare two file and prepare an output file for the matched records.
In case of unmatched records from F1 and F2 I need a write a log file mentioning the unmatched key and corresponding error message in it.
Also after the final completion of the program I need to write the below statistics in log file.

Writing the log file should be done in sub program.

I have the below question. Is file operations possible in sub program. If possible then how can we achieve this.
Should physical declaration of file is needed in sub program. Can we open and close the file in main program or should it be done in sub program.
Show me some sample program.

I will be passing the below details for file write in main program to sub program.



Job Details
Key
details
Program name

Re: Writing a file in sub program

PostPosted: Tue Apr 14, 2015 3:00 pm
by BillyBoyo
There is no problem with doing all the IO, including OPEN and CLOSE, the the sub-program. There are no restrictions in COBOL on what can be in a "main" program (which just happens to be the first one) and a sub-program.

You can open/close in the CALLing program, if your file is defined as EXTERNAL. Check the Enterprise COBOL Language Reference and also the Programmers Guide.

Re: Writing a file in sub program

PostPosted: Tue Apr 14, 2015 3:32 pm
by sachu1912
My doubt is can we open and close the file once in main program and multiple write with the help of sub program.

I have tried this but getting SOc4 during open in main program during opening of file itself.

I have declared the file both in main as well as sub program but opened and closed the file once in main program and for every write I pass values and call sub program for write operation.

Does it need open and close also in sub program?

Re: Writing a file in sub program

PostPosted: Tue Apr 14, 2015 4:32 pm
by BillyBoyo
If you want to open and close in the CALLing program, you need to define the file as EXTERNAL.

A more traditional way is to pass a "flag" with the other parameters, a value for open, a value for close, and a value for do-whatever-else.

Re: Writing a file in sub program

PostPosted: Tue Apr 14, 2015 6:53 pm
by sachu1912
I cannot see any reference for declaring the file as external.
Can you give some simple program to explain the same.Opening and closing in main program and writing in sub program.

Re: Writing a file in sub program

PostPosted: Tue Apr 14, 2015 7:38 pm
by Terry Heinze
Look for EXTERNAL in the COBOL Language Reference Manual Index.