Page 1 of 1
OPEN the file in two different modes in COBOL.
Posted: Mon Jan 30, 2023 10:42 pm
by Richa Chauhan
Hi everyone,
I have a situation which requires me to open the same file in two different mode in a single COBOL program. Can we do that?
Re: OPEN the file in two different modes in COBOL.
Posted: Mon Jan 30, 2023 10:46 pm
by Richa Chauhan
I should have elaborated it. The situation goes like this:
Open a file in the program and if the file is empty write header data and the details to file. However in case the file is not empty we need to write the details only and not the header.
Is there any way to toggle between the modes within the program, let's say: Open the file in input mode, then followed by reading and close it . Later open the file in output mode and do the write? Is it allowed?
Thanks,
Re: OPEN the file in two different modes in COBOL.
Posted: Tue Jan 31, 2023 7:42 pm
by Robert Sample
if the file is empty
You need to be careful here. IBM distinguishes between a data set which has never had any data written to it versus a data set that has been written to but has no records now. In the first case, you cannot read the data set so your application design fails. In the second case, there is no problem with reading.
Furthermore, if you are not specifying DISP=MOD for the data set, then your output statements will replace every record in the data set so you will lose your header data.
Open the file in input mode, then followed by reading and close it . Later open the file in output mode and do the write? Is it allowed?
Yes, it is allowed.
Re: OPEN the file in two different modes in COBOL.
Posted: Mon Feb 06, 2023 2:02 pm
by Richa Chauhan
Robert Sample wrote: Tue Jan 31, 2023 7:42 pmYou need to be careful here. IBM distinguishes between a data set which has never had any data written to it versus a data set that has been written to but has no records now. In the first case, you cannot read the data set so your application design fails. In the second case, there is no problem with reading.
This is very compelling. If a data set was never written to, as you said, how can the data set even exist? Is not it like that it never existed?
Re: OPEN the file in two different modes in COBOL.
Posted: Mon Feb 06, 2023 11:17 pm
by Robert Sample
This is very compelling. If a data set was never written to, as you said, how can the data set even exist? Is not it like that it never existed?
Use JCL utility IEFBR14. This utility allows you to create data sets in JCL, but it does not access that data set. So the data set exists, can be cataloged, but has never been written to. Any JCL can allocate data sets but unless the program being executed opens that DD statement for output, it will not be written to.