Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending records to a file 3

Status
Not open for further replies.

Tiggger

Programmer
Oct 20, 2000
1
US
I s there a way in COBOL to append records to the end of an existing data file? any help with this would be greatly appreciated. thank you. [sig][/sig]
 
Hey Tiggger -

Try OPEN EXTEND when you open the file. This should allow you to append. Hope this helps.

[sig]<p>Christopher Devenny<br><a href=mailto:c_devenny@yahoo.com>c_devenny@yahoo.com</a><br><a href= > </a><br> [/sig]
 
try using OPEN EXTEND for sequential file or OPEN I-O for index/relation data files [sig][/sig]
 
If you're using a utility, e.g. IEBCOPY, specify the first DISP param as MOD instead of
NEW. Thid adds the copied records to the end of the existing file.
 
Swee or anyone else who can help:

I am using RMCOBOL 85 on Linux.

I am using an indexed file and want to add records to it.

&quot;try using OPEN EXTEND for sequential file or OPEN I-O for index/relation data files&quot;

Per your statement above, I am already using OPEN I-O for my file open and my SELECT statement is:

SELECT TEMPDB-FILE ASSIGN TO &quot;TEMPDB&quot;
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD KEY IS TEMPDB-PRIME-KEY
ALTERNATE RECORD KEY IS....
ALTERNATE RECORD KEY IS ..
...
...

OPEN I-O TEMPDB-FILE.
...
..
PROCEDURE.
....
..
CLOSE TEMPDB-FILE.

I get an error when it reaches the OPEN I-O TEMPDB-FILE statement after I run the programs.
The error is a COBOL I/O 35 which in my manual states:

&quot;The file is not available because the file identified by the resultant file access name could not be found. The pathname or filename may be misspelled or may not be valid for the operating system....&quot;

Well, I don't have the file there yet, but I thought it would open and create that file with the name specified in my SELECT statement as I have done that before.

Is it not possible for the OPEN I-O statement to create the file &quot;on the fly&quot; when the OPEN statement is executed if the file is not already there? It seems to work and create the new file when I use simply OPEN OUTPUT, but then every time I perform a WRITE command when I've opened the indexed file with OPEN OUTPUT it &quot;overwrites&quot; the previous record.

How can I make sure to do the following with an indexed file:

1) Create it on the fly when the file does not already exist.

2) Write records to it so that a new record is added and it does not overwrite the last record written to it.

3) This file will start out empty. It will be accessed by various other COBOL programs that will update and delete the records - possibly to the point where no more records are in the file and then more will be added again later. Is this possible with an Indexed or Relative file?

Thanks in advance.
-David
 
If you wish to create the file you have two options (or one depending on the version of the compiler).
1- Test for the status 35 and if you get this one do a
open output
close file
open i-o
within a if.
2- Add the &quot;OPTIONAL&quot; keyword to the select
(select optional filename .....)
 
I'm not sure how helpful this is, but on my mainframe apps, I used
Code:
DISP=(MOD,KEEP,KEEP)
in the JCL, which appends anything you write to the file. As always, it's a good idea to make a backup of the file, &quot;just in case&quot;.

Barring that, I have some older programs which use a GDGs and an inline PERFORM at the begining in which the old records are written to the new generation like this:

Code:
PERFORM UNTIL EOF
        READ old generation
             AT END set EOF to TRUE
        IF NOT EOF
           WRITE to new generation
           END-IF
        END-PERFORM
It gets the job done but looks kinda tacky. :)
 
Okay. Now I want to add a record to an indexed file.
THis indexed file has a primary key PIC 9(11).

Do, I have to somehow find the last record's primary key value, add 1 to that value into a WS variable and use that WS variable value before writing my new record to the end of that indexed file?

Thanks.
-David
 
My RMCOBOL Manual states:
(Page 6-214)
For an indexed file, the data item specified as the prime record key must be set by the
program to the desired value prior to the execution of the WRITE statement.

So, I'm wondering the quickest way to do a read at the end of an indexed file, and grab the last record's primary key value.
This way, I'll simply increment that value by 1 and be able to write my new record to the indexed file. Per the Manual snippet above, I need to move a value into the prime record key.
For some reason, I am thinking that a START statement would point in the right spot of the file, but I'm not sure.
Thanks.
-David
 
David,
Code:
OPEN I-O TEMPDB-FILE.
START TEMPDB-FILE KEY IS LAST
   INVALID KEY ...
END-START.
READ TEMPDB-FILE NEXT ...

and you will have the record containing the highest value for the prime key.

Tom Morrison
 
Wow, that's exactly what I tried, but I think it forced me to put the name of the key after the 'LAST' keyword.
But, I'm also glad I asked, because I was putting in a &quot;READ TEMPDB-FILE PREVIOUS RECORD&quot; statement afterwards.
Per your statement above using the &quot;READ TEMPDB-FILE NEXT...&quot; I assume my START TEMPDB-FILE KEY IS LAST TEMPDB-PRIME-KEY will put the pointer right before the last record, so that when I do a READ NEXT it is on the last record.
I assumed the 'LAST' would land me right at the very end of the file and I would have to do a &quot;READ ... PREVIOUS RECORD&quot; statement.

Thanks, Tom!
-David
 
Here is a test program:
Code:
       identification division.
       program-id. start-last.
       environment division.
       input-output section.
       file-control.
           select optional testlast assign random
               organization indexed
               access dynamic
               record key is testlast-key.
       data division.
       file section.
       fd  testlast.
       01  testlast-record.
           02 testlast-key.
              03  pic x(1).
           03     pic x(10).

       procedure division.
       a.
           open i-o testlast.
           move all &quot;9&quot; to testlast-record.
           write testlast-record.
           move all &quot;7&quot; to testlast-record.
           write testlast-record.
           move all &quot;1&quot; to testlast-record.
           write testlast-record.
           move all &quot;5&quot; to testlast-record.
           write testlast-record.
           move all &quot;3&quot; to testlast-record.
           write testlast-record.
           start testlast key is last testlast-key.
           read testlast next.
           display testlast-record.
           stop run.

and the results:
Code:
RM/COBOL Runtime - Version 8.00.00 for 32-Bit Windows.
99999999999
COBOL STOP RUN at line 34 in program START-LAST


Tom Morrison
 
Okay. Thanks, Tom.
I'll test my program. Great example above. It illustrates well the usage of 'LAST'. It also emphasizes the fact that although '9999..' was written first, it is found at the end, since it is indexed.

Thanks.
Star to Tom.
-David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top