OPEN.Error routines.
CLOSE.
READ.
WRITE.
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
ENVIRONMENT DIVISION.
input-output section.
file-control.
(a) select outfile assign "OUTFILE"
(b) organization is sequential
(c) status is file-error.
DATA DIVISION.
(d) fd outfile.
(e) 01 out-record.
03 rec-type pic 99.
03 filler pic xx.
03 amount pic zz,zz9.99.
* ------ variable declarations.
working-storage section.
(f) 01 file-error pic 99.
There must be a SELECT for every file. The name immediately after the SELECT is the name which 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 let the physical file name appear in a variable in the working storage section.b
There are two ways of organizing sequential files: RECORD, as default, and LINE. A LINE sequential file is the same as an ASCII text file; a RECORD sequential file cannot contain separators between records.c
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. The file can contain a mix of records of the various layouts.f
The variable to the status code must be declared in the working-storage section, as stated in the manual. The data-type of this variable can vary between different compilers.
[This chapter] [TOC] [Tutorial]
OPEN.
CLOSE.
READ.
WRITE.
[This chapter] [TOC] [Tutorial]
OPEN EXTEND file-name ....You can only write records to the file, and all new entries will be placed as an extension of the file's present contents. If required, a new file is created.
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 INPUT text-file.An example of how to handle sequential files.
[This chapter] [TOC] [Tutorial]
CLOSE file-name ....
CLOSE text-file.An example of how to handle sequential files.
[This chapter] [TOC] [Tutorial]
READ file-name [INTO record-name] [AT END imperative-statement-1] [NOT AT END imperative-statement-2] [END-READ]
READ text-file AT END SET eof TO TRUE.
you read a file, but write a record.Explanation:
A sequential file can contain records with a number of different record-layouts. When you write data to a file, you specify what layout that is used for the new record. When you read from a file, you have no way of knowing the layout of the next record in the file. You have to determine the layout after the record is read, preferably by some type-tag in the record.
[This chapter] [TOC] [Tutorial]
WRITE record-name [FROM identifier-1] [END-WRITE]
WRITE record-name [FROM identifier-1]
[{BEFORE | AFTER } ADVANCING {identifier | integer} [LINES]]
[END-WRITE]
OPEN OUTPUT text-file. WRITE text-line BEFORE 1 LINE.
you read a file, but write a record.Explanation:
A sequential file can contain records with a number of different record-layouts. When you write data to a file, you specify what layout that is used for the new record. When you read from a file, you have no way of knowing the layout of the next record in the file. You have to determine the layout after the record is read, preferably by some type-tag in the record.An example of how to handle sequential files.
[This chapter] [TOC] [Tutorial]
* ------ Trap for IO-errors (a) declaratives. (b) in-errors section. (c) use after error procedure on in-file. (d) in-error. (d1) display "Error when accessing file : " (d2) line 24 position 1. (d3) display file-error line 24. (d4) stop run. (e) in-error-out. (f) exit.
The word "declaratives" is header for the error-handling part of the source code. The word can only be stated once, just after "procedure division".b
You declare one section for each trap you want to set up. You name these sections after the normal naming rules for sections and paragraphs.c
"use" describes in which connection the error procedure is to be used. In this case all error conditions on in-file is handled by this routine.d
A normal paragraph name.d1-d4
The actual code for handling the problem. You can write all most any instructions here, but the purpose is to minimise the problems caused by the error. Line d1-d4 simply report the error on line 24 of the screen. Line d4 terminates the program. In this case the program is interrupted by any type of error. 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.e+f
Just a nice way to say the routine end here. Another declarative-section may follow.
[This chapter] [TOC] [Tutorial]
identification division.
* ------ program identification.
program-id. seqfile.
author. kik.
environment division.
* ------ program environment.
configuration section.
special-names.
console is crt.
input-output section.
file-control.
select in-file assign "IN-FILE"
organization is line sequential
status is file-error.
select out-file assign "OUT-FILE"
organization is line sequential
status is file-error.
data division.
* ------ description of files.
fd in-file.
01 in-record.
03 rec-type pic 99.
03 amount pic 99999v99.
fd out-file.
01 out-record.
03 rec-type pic 99.
03 filler pic xx.
03 amount pic zz,zz9.99.
* ------ variable-declarations .
working-storage section.
01 file-error pic 99.
01 eof-code pic 9 value 0.
88 eof value 1.
01 last-rec-type pic 99 value zero.
01 total pic 9(6)v99 value zero.
procedure division.
* ------ Trap for IO-errors
declaratives.
in-errors section.
use after error procedure on in-file.
in-error.
display "Error when accessing input data : " line 24
position 1.
display file-error line 24.
stop run.
in-error-out.
exit.
*
out-errors section.
use after error procedure on out-file.
out-error.
display "Error when opening/writing output data : "
line 24 position 1.
display file-error line 24.
stop run.
out-error-out.
exit.
end declaratives.
*
* ------ executable instructions.
main section.
perform open-files.
perform read-post.
move rec-type in in-record to last-rec-type. perform process
until eof.
perform write-total.
perform close-files.
stop run.
*
*
open-files.
open input in-file.
open output out-file.
*
read-post.
read in-file
at end move 1 to eof-code.
*
process.
if not rec-type in in-record = last-rec-type then perform write-total.
add amount in in-record to total.
perform read-post.
*
write-total.
move total to amount in out-record.
move last-rec-type to rec-type in out-record.
write out-record before 1.
move zero to total.
move rec-type in in-record to last-rec-type.
*
close-files.
close in-file,
out-file.
[This chapter] [TOC] [Tutorial]
- COBOL's facilities for handling sequential files
- Selections in COBOL
- Calculations
- Numeric editing
- Output data to the terminal.
- Read the file described below
- Provide a total for each value in the field TR-CODE
- Present the result on the screen.
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 is not to be sorted.Here is an extract of the file:
The "Movement" field should only apply to transaction codes 1-4.
The "Balance" field should only relate to transaction code 99.
4711010001230000000000 7100020011002500000000 5500990000000000043200