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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

updating file with one record

Status
Not open for further replies.

confusedlady

Programmer
Apr 26, 2001
163
US
The program I'm working on uses several files.One of them contains only one record, and with each successful addition to another file, a value in this record is incremented and the record should be re-written.

I have the organization and access defined as sequential.
The first update is fine, but for the next update, I get an EOF condition.I know this is due to me trying to read the file again, but I don't know how to fix it.

Is there a way to specify that the same record be re-read? Or at end go to the beginning-something like that?

Any help would be appreciated.

confusedlady
 
confused,

You really do not need to update (re-write) everytime you increment a counter in the record...

Process all the files you need to process; and perform all the corresponding increments you need for every file you process; and then when you have no more files to process, re-write this single record... no special magic, no hassles, no extra-ordinary file processing, no problem!

Good luck.
 
That would be easier, Winzip, but the file needs to be accurate all the time.The program is an online maintance one (which can be accessed by many users)that adds records, but also changes them.Say, for instance, you add #1000,1001,1003 and at the same time, another user wants to pull up #1000 again to change it, it can be done. confusedlady
 
Oops, that should be you add #1000,1001,1002
I can't count..... confusedlady
 
confused,

I'm afraid you lost me there...

I thought you said this is a single-record file; sequential org and access; then you mention about 1000, 1001, 1002 and going back and pulling up 1000 again? What's there to pull up again? I think I have an idea of what you really mean, but then again if you made your example clearer then I wouldn't have to guess...

Then also, consider your scenario: a single-record sequential file, sequentially accessed, update-in-place, in an online environment? With multiple users and potential simultaneous reads/rewrites?

I'm sorry but I need a little more context and a clearer description of what your program wishes to achieve...
 
I apologize, Winzip.I'll try to start over and explain better...

The pgm is an online maintance pgm that should have the ability to add or change records in a "master" file, which is keyed by an ID number.When a record is added, the next successive # is assigned when the record is written.In addition to writing this new record, the "control file", which has only one record is updated with the new ID number. Say userA adds a record (1001).The control file now has 1001 in the ID field.Two seconds later, userB hits the update key to add his record.He should see #1002 assigned and the "control" file is rewritten to reflect this.

I hope this is clearer! confusedlady
 
If you are going to use this file to look something up and you expect the log file could get lots of entries in it, maybe the file should be a keyed file?

However, if all you want to do is write records to a file, all you have to do is read the file till you know what the last record in the file is and put that in a counter. This will cause you to reach the end of file indicator. When you reach the end of the file you just set the counter. Each time you want to add a record increment the counter and move the value to the record change what you want and write a record. When you write a record to a file it always is added to the end of the file anyway. The only exception is if you randomly find a record and then rewrite it. Since you are always writing a record with a new and unique key write it to the end of the file as normal, that way the file is in natural key order.

If you could let the key be the record number that might be nice. Some file systems might let you do that. I know that VSAM keyed files have 2 part keys one being the actual record number. I don't know if this is accessable or not. If you could build a table instead of a file, the number could be auto-generated.

You may want another key such as date and time if you are going to be looking up or searching the log entries. If you do not like my post feel free to point out your opinion or my errors.
 
confused,

What complicates things for you is the environment you want to do this in...

I am assuming when you say ONLINE you mean you are operating in a mainframe environment, probably CICS... if that is the case then 'sequential' will not cut it... as ceh4702 suggests, you will be better off having a keyed file. It doesn't matter that the file has only one record - that is done all the time. And so in such a case it really won't matter how you define the key - since there is just one record.

The program will then perform a read (actually you do a start browse, a read, then an end browse); and then update the record in place - on a keyed file, you don't have to worry about going beyond EOF with each access. What you need to take care of is record locking so that conflicting/concurrent updates are controlled.

Hope this helps.
 
Confused,
Not sure if I have this right but it sounds as if you are trying to write something that we use in work here quite a bit - for example a lot of online users on one system need a case number generated - these case number uniquely identify a customer and are sequential. If this is the case then a sequential read/rewrite is not what you are looking for. Depending on your environment/tools you have would suggest using either an IMS/DLI call - GHU the segment containg the number (so no-one else can access it whilst you are updating), increment the counter and then REPL the segment (replace), then checkpoint - this will free up the segment for the next operator. Alternatively use DB2 with a cursor (get the row, update the row and release the lock with a commit)

Hope of some use

Mike
 
Thanks all for the help.I am fairly new to programming (especially AS/400 environment). Part of the problem is that I did not set up the file structures for the input files for this app. The one with the single record was set up as non-keyed. I spent numerous hours trying to figure out how to make that do what I want (ie. avoid the EOF issues), but it seems that you all have confirmed that it should, indeed, be a keyed file. Thanks a bunch! confusedlady
 
I have never seen an online system on an AS400, but I have written one little program on an AS400 that had an interactive screen for a class. All it did is read a file based on the customer number and average the account balance, and printed it out as a graph. We used COBOL for the AS400 and compiled our own DD File Copy and our own screen files. On an AS400 you may be able to edit a file by the record number of the file. When we created files we used the file edit utility and it showed the record number. The AS400 handles files as if they were a database. I beleive on the AS400 to find a record on a keyed file you just move the value to the key field and read the file like so:

move temp-key to in-rec-key.
read log-file
invalid key
<imperative statement>
not invalid key
<imperative statement>
end-read.

I did not use the normal end-of-file indicator. I used a screen and allowed the F3 key to end the program. I was quite surprised it actually worked.

I am curious what kind of online system you use on the AS400. Do you use CICS or something else? I would guess that if you have multiple users hitting the same file you have to hadle that in some special way.

Charles Hammond. If you do not like my post feel free to point out your opinion or my errors.
 
I wish I could answer some of those questions,ceh4702! I'm kind of &quot;muddling through&quot;, though, picking it up along the way!

I defined the display file/indicators in the DDS (AS400 also provides SDA-screen design aid)for the file. Subfile processing isn't utilized extensively in this app (thankfully), or both the subfile and subfile control record would be defined in this display file.
The other input files had already been created/defined (which was a lot of the problem).

The main loop is as you described - exit point is when F3 is pressed,so the coding for the Proc Div was logically similar to that for a batch pgm-as far as the looping construct anyway.

For a keyed file, the AS400 data entry utility permits you to locate the record by entering the key field(s)(utility for that, too!)



confusedlady
 
This is not terribly efficient, but it's guaranteed to work, even without restructuring the file. CLOSE the file and re-OPEN it after the WRITE. Betty Scherber
Brainbench MVP for COBOL II
 
Thanks, Betty. You're right-it isn't very efficient, so I was trying to avoid doing it, but that is my &quot;if all else fails...&quot; X-)

confusedlady
 
confused & betty,

hold it! i thought you said this file is online and accessible to multiple users... then what is this business of closing then reopening the file? do you know how much confusion that can cause in an online environment? are these real-world situations you are consulting us with, or are these school case studies (although i must say i thought the days of school case studies like these have been long gone)?
 
School case studies? No. It IS in an online environment, but this is not the main, or master-type file. I am sure that I'm just not describing this correctly, so I'll try yet again Winzip...

The &quot;master&quot; file is open for the entire session.Say user A adds a record to that master file. The next ID # is assigned to the record, and this single-record file is rewritten-the only field that changes is the ID # (so it always has the current value). A few minutes later, user B adds a record. That single-rec file is read only to get the current ID#, and one to it and rewrite it-it's locked only briefly. That's it.

I ended up changing the file structure to a keyed field, using a dummy control field as the key. Now that the file isn't sequential, I was able to use the read first record form of the read statement, and all is working fine.But, like I said,it could have been accomplished by opening/closing that file repeatedly, without a noticable difference to the user. confusedlady
 
hi confusedlady!

maybe i'm the one who's not being clear. i understand that your master file is the only one 'visible' to the user. i also understand that the 'control' file gets updated only when a 'change' happens to your master file.

what i am saying however is, opening and closing your 'control' file is NOT a viable idea.

picture this: user A does something; control file gets updated, closed, then reopened... in the meantime user B goes in and does something; the control file is read - BUT - wait a minute, it so happens the control file is closed at that instant... you know what happens? ABEND!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top