OPEN.Error routines.
CLOSE.
READ.
READ ... KEY.
READ ... NEXT.
READ ... PREVIOUS.
START.
WRITE.
REWRITE.
DELETE.
DELETE FILE.
UNLOCK.
COMMIT.
Exclusive.An example of how to handle index-sequential files.
Shareable.
Single record lock.
Multi-record lock.
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
ENVIRONMENT DIVISION. input-output section. file-control. (a) select isamfile assign "INOUTFILE" (b) organization is indexed (b1) access is random (b2) record key is acctno (c) status is file-error. DATA DIVISION. (d) fd isamfile. (e) 01 in-record. 03 acctno pic 99. 03 amount pic 99999v99. * ------ variable declarations. working-storage section. (f) 01 file-error pic XX.
There must be a SELECT for every file. The name immediately after the SELECT is the name that is to be used internally by the program; on the other hand, the name after ASSIGN is the physical file name. It is of course possible to operate with a variable in the working storage section.b
Index-sequential files are organized using the word "indexed".b1
The access for an index-sequential file can vary, depending on the purpose of the program:b2
Random All access occurs via the given key value. Sequential Records must be read sequentially, the order in accordance with given key. Dynamic If you wish to "mix" the first two access forms.
The compiler uses this information to decide the position of the key in the record. It is possible to specify a higher level when operating with a split key. A second method of specifying a compound key to be used when the fields are not in the correct order is as follows:c
record key is split-key = field1, field2, ...Where necessary, specify alternative keys with:
alternate key is [split-key = ]field1[, field2,..]If a key is not unique, specify it:
with duplicatesafter the relevant key.
The variable file-error will contain a status code after each file operation. See the manual for details of actual codes.d
There must be an FD for every file. The name after FD is the same as that written after the SELECT in line a.e
A number of different record descriptions can be added after the FD.f
The variable to the status code must be declared in the working-storage section, as stated in the manual.
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
OPEN [WITH LOCK] {INPUT | OUTPUT| I-O} file-name
OPEN OUTPUT file-name ....You can only write records to the file. The file will be created. If the file all ready exists it will be deleted and a new file created.
OPEN INPUT file-name ....You can only read data from the file. Depending on the word "OPTIONAL" in the file's SELECT statement, a missing file will give an IO error on being opened or merely give an "AT END" the first time it is read.
OPEN I-O file-name ....You can retrieve, insert and update data in the file. Usually you can not open a file in this mode unless it previously have been created by an open output.
[This chapter] [TOC] [Tutorial]
CLOSE file-name.File is closed and all locks released.
[This chapter] [TOC] [Tutorial]
READ file-name.The record that meets the key conditions is searched for and read.
[This chapter] [TOC] [Tutorial]
READ file-name KEY key-nameThe record that meets the key conditions is searched for and read.
[This chapter] [TOC] [Tutorial]
READ file-name NEXT.Reads the record after that searched for. The current key concept decides which record will be the next.
[This chapter] [TOC] [Tutorial]
READ file-name PREVIOUSAs READ ... NEXT, but now the previous record.
[This chapter] [TOC] [Tutorial]
START file-name ....This instruction is used to position the file prior to the execution of a subsequent sequential read command. See the manual for variations of this command.
[This chapter] [TOC] [Tutorial]
WRITE record.Add a new record to the file. Updates index for all keys.
[This chapter] [TOC] [Tutorial]
REWRITE record.Update a current record, usually the last. Updates index for all keys.
[This chapter] [TOC] [Tutorial]
DELETE file-name.Deletes a current record, usually the last. Updates index for all keys.
[This chapter] [TOC] [Tutorial]
DELETE FILE file-name.Deletes entire file. The file must be closed.
[This chapter] [TOC] [Tutorial]
UNLOCK file-name.Releases all locks in the file.
[This chapter] [TOC] [Tutorial]
COMMIT.Releases all locks in all files in this program.
[This chapter] [TOC] [Tutorial]
* ------ Trap for IO-errors declaratives. in-errors section. (a) use after error procedure on isamfile. in-error. display "Error when accessing directory : " line 24 position 1. display file-error line 24. (b) stop run. <- remove this line and the error will be ignored. in-error-out. exit.Comments on the example:
use describes in which connection the error procedure is to be used.
In this case the program is interrupted by any type of error - including locked record. Alternatively, the program could be allowed to continue immediately after the statement that triggered the error. In the program itself it is therefore important to make sure you "tidy up" after an error - either via declaratives or via the various I/O statements.
[This chapter] [TOC] [Tutorial]
The locking program has exclusive rights to the file while ever the lock is in place. This lock can therefore only be used if other programs have not opened the file.
This allows several programs to share the contents on the file at the same time, but to also operate with mutual protection against overwriting records.
[This chapter] [TOC] [Tutorial]
- Directly specify in a file's SELECT statement that it is to be locked.
- By using OPEN OUTPUT.
[This chapter] [TOC] [Tutorial]
The last read record can be locked. Exclusive is the usual choice when writing in sequential files.For ISAM and relative files
See the table below.
The difference between an ISAM and a relative file is that the record in an ISAM file contains a key, while the key to a relative file is the record's relative number.
Single record lock allows the program to operate with a lock on only one record at a time.
With Multi-record lock it is possible to lock several records in one file at the same time.
Single record lock. | |||||
Manual locking | Automatic locking | ||||
A record is locked when it is read with | and remains locked until one of the following instructions is used | Automatic locking is specified in the file's SELECT statement. Each READ will lock the read record, which is then released as described for manual locking. | |||
READ WITH LOCK | UNLOCK file-name COMMIT CLOSE file-name READ file-name WRITE record |
||||
Multi-record lock | |||||
Manual multi-locking | Automatic locking | ||||
A record is locked when read with | and remains locked until one of the following instructions are used | Automatic locking is specified in the file's SELECT statement. Each READ will lock the read record, which is then released as described for manual locking. | |||
READ WITH KEPT LOCK | UNLOCK file-name COMMIT CLOSE file-name |
[This chapter] [TOC] [Tutorial]
identification division. * ------ program identication. program-id. isamfile. author. kik. environment division. * ------ program environment. configuration section. special-names. console is crt decimal-point is comma. input-output section. file-control. select isamfile assign "INOUTFILE" organization is indexed access is random record key is acctno status is file-error. data division. * ------ description of files. fd isamfile. 01 in-record. 03 acctno pic 99. 03 amount pic 99999v99. * ------ variable declarations working-storage section. 01 file-error pic XX. procedure division. * ------ Trap for IO-errors declaratives. in-errors section. use after error procedure on isamfile. in-error. display "Error when accessing diectory: " line 24 position 1. display file-error line 24. stop run. in-error-out. exit. * end declaratives. * * ------ executable instructions. main section. perform open-files. perform show-screen. perform read-acct. perform process until acctno in in-record = zero. perform close-files. stop run. * show-screen. display space. display "Index-sequential files" line 1 position 25. display "Account number ..:" line 4 position 10. display "Balance .........:" line 6 position 10. * open-files. open i-o isamfile. * process. read isamfile invalid key move zero to amount in in-record not invalid key display amount in in-record line 6 position 26 end-read. perform read-amount. rewrite in-record invalid key write in-record. perform read-account. * read-account. accept acctno in in-record line 4 position 26. * read-amount. accept amount in in-record line 6 position 26. * close-files. close isamfile.
[This chapter] [TOC] [Tutorial]
- COBOL's facilities for handling sequential files.
- COBOL's facilities for handling index-sequential files.
Transfer the sequential file to a corresponding index-sequential file.
The same as for "SEQUDATA" above.
Customer number, 4 characters, value set: 0-9999.
Transaction code, 2 characters, value set: 1, 2, 3, 4, 99.
Movement, 6 characters + 2 decimals (no decimal point).
Balance, 6 characters + 2 decimals (no decimal point).
The file should be sorted according to customer and transaction code.
[This chapter] [TOC] [Tutorial]
- COBOL's facilities for handling index-sequential files in a sequential mode.