Writing a file in sub program



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

Writing a file in sub program

Postby sachu1912 » Tue Apr 14, 2015 1:12 pm

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
sachu1912
 
Posts: 7
Joined: Thu Apr 09, 2015 4:01 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Writing a file in sub program

Postby BillyBoyo » Tue Apr 14, 2015 3:00 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Writing a file in sub program

Postby sachu1912 » Tue Apr 14, 2015 3:32 pm

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?
sachu1912
 
Posts: 7
Joined: Thu Apr 09, 2015 4:01 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Writing a file in sub program

Postby BillyBoyo » Tue Apr 14, 2015 4:32 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Writing a file in sub program

Postby sachu1912 » Tue Apr 14, 2015 6:53 pm

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.
sachu1912
 
Posts: 7
Joined: Thu Apr 09, 2015 4:01 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Writing a file in sub program

Postby Terry Heinze » Tue Apr 14, 2015 7:38 pm

Look for EXTERNAL in the COBOL Language Reference Manual Index.
.... Terry
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