OPEN the file in two different modes in COBOL.

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Richa Chauhan
Registered Member
Posts: 18
Joined: Sat Sep 27, 2014 9:13 pm

OPEN the file in two different modes in COBOL.

Post 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?
Richa Chauhan
Registered Member
Posts: 18
Joined: Sat Sep 27, 2014 9:13 pm

Re: OPEN the file in two different modes in COBOL.

Post 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,
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1896
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: OPEN the file in two different modes in COBOL.

Post 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.
Richa Chauhan
Registered Member
Posts: 18
Joined: Sat Sep 27, 2014 9:13 pm

Re: OPEN the file in two different modes in COBOL.

Post 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?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1896
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: OPEN the file in two different modes in COBOL.

Post 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.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”