View previous topic :: View next topic
|
Author |
Message |
Chiku kumar
New User
Joined: 21 Jun 2018 Posts: 4 Location: India
|
|
|
|
Can we use a file both as input and output in COBOL program
example: We need to search the file if record not present write to that same file
Thanks
Topic cleaned: By random admin, not me :-P |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8797 Location: Welsh Wales
|
|
|
|
Your explanation of the requirement is too vague to answer the question.
What do you write to the same DATASET, which type of DATASET are you talking about, VSAM ESDS, VSAM KSDS, VSAM RRN sequential on DASD, sequential on tape, PDS, PDSE ....................
Are we using DB2 or IMS or ADABAS or what ???
Each and every option may possibly have a different solution.
Please explain with lots of deatil what you wish to achieve. |
|
Back to top |
|
|
steve-myers
Active Member
Joined: 30 Nov 2013 Posts: 917 Location: The Universe
|
|
|
|
Yes, but it's not a good idea. Any mistake in your program will effectively destroy the data set. Notice I say "data set." The only "files" in OS/360 derived systems like MVS or z/OS is data on magnetic tape. A "file" as used in *nix or Windoze ia just a stream of bytes. A "data set" is also a stream of bytes organized into records and possibly higher level structures as in a partitioned data set or a VSAM data set.
In general it is not possible to insert data into a sequential data set (as you appear to propose) except by rewriting the data set from the beginning. It is possible, though somewhat difficult, to update data in a sequential data set. |
|
Back to top |
|
|
Chiku kumar
New User
Joined: 21 Jun 2018 Posts: 4 Location: India
|
|
|
|
Its a sequential on DASD file which I want to search first using SEARCH option in COBOL and then I want to write a record into the same file IF the record doesn't already exist in the file (Unique records should be written to the file). |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Quote: |
Its a sequential on DASD file which I want to search first using SEARCH option in COBOL |
The SEARCH verb in COBOL applies ONLY to tables -- not data sets, not files, only tables. Hence your basic premise is flawed.
Quote: |
then I want to write a record into the same file IF the record doesn't already exist in the file (Unique records should be written to the file). |
What you want to do is not possible. From the Enterprise COBOL Language Reference manual version 6.2 page 489 with emphasis added by me:
Quote: |
WRITE statement
The WRITE statement releases a logical record to an output or input/output file.
When the WRITE statement is executed:
v
The associated sequential file must be open in OUTPUT or EXTEND mode.
v
The associated indexed or relative file must be open in OUTPUT, I-O, or EXTEND mode. |
And since you cannot read from a COBOL file opened for OUTPUT or EXTEND mode, it is therefore obvious that you ask for the impossible. It could be done with a VSAM KSDS but not for any form of sequential data set. |
|
Back to top |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
Chiku Kumar,
You have to open in Input mode, then Close and again open in Extend mode
. |
|
Back to top |
|
|
jasorn Warnings : 1 Active User
Joined: 12 Jul 2006 Posts: 191 Location: USA
|
|
|
|
Just want to confirm this is a bad idea except with vsam files. Depending on various factors, there are cases where you CAN do this with COBOL and other programs, but it's really a bad idea for so many reasons.
Do yourself and your support staff a favor and avoid writing to the input file except for vsam. 'File' as in defined as input in the 'FILE SECTION'. |
|
Back to top |
|
|
|