The COBOL-85 Tutorial, Chapter 6

Sequential files.

Copyright © 1998-2017

Kim Kjærsulf.


This chapter (TOC)

Sequential file.
Declaring a sequential file.
Commands.
OPEN.
CLOSE.
READ.
WRITE.
Error routines.
An example of how to handle sequential files.
Exercises

Sequential file.

[This chapter] [TOC] [Tutorial]


A sequential file in COBOL can briefly be described as a file without a key concept. The most obvious example is text files (ASCII), but COBOL can also handle files in a binary format such as sequential - these will however be characterised as files with fixed record lengths though not necessarily a fixed record layout.



Declaring a sequential file.

[This chapter] [TOC] [Tutorial]


Before a file can be used in a COBOL program its organization has to be defined, partly in an ENVIRONMENT DIVISION, where the physical file name and organization is stated, and partly in a DATA DIVISION, which is used to describe the record layout.

Below is an example of a description of a sequential output file; for further details, see the manual.

                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.

Comments on the example:

Line Explanation

a
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.

An example of how to handle sequential files.

This chapter

Commands.

[This chapter] [TOC] [Tutorial]


Most of the commands for handling and accessing sequential files, are the same for both LINE and RECORD files. There are functions for sharing files between different simultaneous users, but this topic will be dealt with in a later chapter.

OPEN.
CLOSE.
READ.
WRITE.


This chapter

OPEN.

[This chapter] [TOC] [Tutorial]


You have to execute an OPEN statement prior to any use of the file, since this is the way to establish contact between the program and the disc-file.

You have to select the OPEN format depending on what operations you are going to perform on the file: create a new file, extension of an existing file or just reading.

	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.

Example :

		OPEN INPUT text-file.
An example of how to handle sequential files.


CLOSE.

[This chapter] [TOC] [Tutorial]


When your program no longer need access to an open file, you must execute the CLOSE statement. Especially when you have been writing to a file it is important not to forget a close.

If you want to change the way a file is opened, you have to close it before opening it in a new mode. For example if you have been writing data to a temporary file, and then wants to read the data.

As long as files only are placed on a disc (as oppose to tape), there are only one way to close.

	CLOSE file-name ....

Example :

		CLOSE  text-file.
An example of how to handle sequential files.


READ.

[This chapter] [TOC] [Tutorial]


You use the READ instruction when you want to read one line or record from the file. The first time a READ is executed after an OPEN, you will read the first line or record in the file. Each succeeding READ will advance the file one line/record until end of file is reached.

When you try to read after the last line/record in the file, the AT END clause will take effect. I.e. you do not check to see when end of file is reached, you simply perform reads until your program passes the end of the file.

The data read from the file is placed in the record description under the files FD-entry. If you want to move the data into a variable in the working-storage section you can use the INTO option. The "INTO" is a MOVE-instruction that simply copies the data from the FD into an appropriate record in working-storage.

	READ file-name [INTO record-name]
		[AT END imperative-statement-1]
		[NOT AT END imperative-statement-2]
		[END-READ]

Example :

		READ  text-file 
			AT END SET eof TO TRUE.

An example of how to handle sequential files.

Worth remembering:
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.


WRITE.

[This chapter] [TOC] [Tutorial]


You use the WRITE instruction when you want to insert a new line or record into the file.

When you write to a record sequential file, you must use the simple version of the WRITE- instruction:
WRITE record-name [FROM identifier-1]
		[END-WRITE]

When you write to a record sequential file, you can specify a number of line breaks that must be done before or after the newly written line :

	WRITE record-name [FROM identifier-1]
		[{BEFORE | AFTER } ADVANCING {identifier | integer} [LINES]]
		[END-WRITE]

Example :

		OPEN OUTPUT text-file.
		WRITE  text-line BEFORE 1 LINE.

Remember that there are many variations of WRITE, depending on whether you wish to write to a pure ASCII file or whether it is a print file.

Worth remembering:
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.


Error routines.

[This chapter] [TOC] [Tutorial]


Errors occurring in file operations can be detected with procedures written in the "DECLARATIVES" part at the beginning of the procedure division.

It is possible to operate with a general procedure, a joint procedure for all input and output files, or one procedure per file.

The example below deals with all errors relating to a file called "in-file":

	* ------  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.

Comments on the example:

Line Explanation

a
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.

An example of how to handle sequential files.


An example of how to handle sequential files.

[This chapter] [TOC] [Tutorial]


This program is an example of a program that reads a sequential file, processes data, and then writes these to a new sequential file.

The program reads every record in a file, and adds an amount from every record. The file is ordered by a record type, and each time the program encounter a new record-type in the input file, the program writes a total to the output file. The final result is a line in the output file for each record-type, with the sum of amounts for that type.

       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.

Exercise 1.

[This chapter] [TOC] [Tutorial]


The purpose of this exercise is to train the following points:

Design a program with which you can:

File description:

Physical file-name: "SEQUDATA"

Record layout:
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).

Comments:
The file is not to be sorted.
The "Movement" field should only apply to transaction codes 1-4.
The "Balance" field should only relate to transaction code 99.
Here is an extract of the file:

	4711010001230000000000
	7100020011002500000000
	5500990000000000043200

Model answer for this exercise.


This chapter

Copyright © 1998-2017

Kim Kjærsulf


Last Updated october 3, 2003
For more information contact: Kim Kjærsulf