FILE-CONTROL Paragraph [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation
HP COBOL II/XL Reference Manual
FILE-CONTROL Paragraph
The FILE-CONTROL paragraph is used to name files to be used in your
program, and to define certain properties of these files that are
necessary for their use by your program. Each file named in the
FILE-CONTROL paragraph must be described in the DATA DIVISION of your
program. Conversely, each file described in the DATA DIVISION must be
named once in the FILE-CONTROL paragraph.
An overview of the types of files that can be used in HP COBOL II is
presented on the following pages. Following this overview, the various
clauses of the FILE-CONTROL paragraph are described.
In HP COBOL II, there are five ways to access and use files:
* Sequential access
* Random access
* Relative access
* Indexed access
* Sort-merge access
Each type of file described must be named, and certain features
specified, in the INPUT-OUTPUT SECTION. The organization of the files and
their logical records must be described in an SD entry (for sort-merge
files), or an FD entry (for any other type of files) in the FILE SECTION
of the DATA DIVISION.
Sequential Files
Sequential files are generally files residing on, or being written to, a
serial access device (such as a magnetic tape or a serial disk). Of
course it is possible to access disk files sequentially also.
A sequentially accessed file means the records of that file can only be
accessed in the order in which the records were written to the file.
Because of the nature of serial access devices, these types of files can
only be written to or read from in a single operation. However, on
direct access discs, files being accessed sequentially can be read from
and written to at the same time, as well as have a record brought in,
modified, and returned to the same storage area.
Random Access Files
Random access files must reside on disk. Through the use of a key, you
can read or write a record anywhere within a random access file,
regardless of whether data has been written on previous records.
The only limitation on where you can write a record is the externally
defined boundaries of the file. For example, if a random access file has
been defined to contain a maximum of three thousand records, you cannot
write data to record number 3010, although you can write data to record
number 2000 without having written data to any preceding records.
Record access of random access files is controlled by the data item
defined in the ACTUAL KEY clause of the FILE-CONTROL paragraph. This
data item is described anywhere other than in records associated with
this file. It is most efficient if defined as a signed integer of five
to nine digits whose usage should be described as COMPUTATIONAL
SYNCHRONIZED. However, any numeric data item of a sufficient size for the
records in a file can be used.
The ACTUAL KEY data item is used by placing a number into it that
corresponds to the logical record number in the file. Logical records in
a random access file begin with record zero. Thus, to access the tenth
record in a random access file, your program must move the integer 9 into
the ACTUAL KEY data item, and then execute the input-output statement.
Execution of an input-output statement for random access files does not
update the ACTUAL KEY data item. For example, if the ACTUAL KEY data
item contains a value of one before reading or writing takes place, the
second record is accessed when the READ or WRITE statement is executed.
Following execution of the statement, any subsequent WRITE statement or
READ statement (without the NEXT phrase) also accesses record one, unless
the value in the ACTUAL KEY data item has been changed.
When your program writes data to a record of a random access file, and
previous record areas have had no data written to them, these records are
filled with blanks or zeroes. Blank fill is used when the records of the
file are designated as ASCII records. Zero fill is used when the records
are designated as binary records.
The implication of this blank/zero filling is that you can read records
of the file for which no WRITE statement has been executed. In such a
case, the data moved into your program is either a blank record, or a
zero-filled record. This capability does not exist for any other type of
file.
Relative Files
Relative files are similar to random access files in that you access
records of such a file through the use of a record number. The only real
difference in the two keys used for random access and relative files is
that record numbers on a relative file begin with one, rather than with
zero as in random access files.
The major functional difference between random access and relative files
is that you can always reuse record areas in a random access file by
simply writing new data in them. In relative files, you must use the
DELETE statement to purge data from record areas. Once a record has been
deleted, you can no longer access the area it occupied except to write a
record into it again.
Relative files opened in dynamic mode use a data item named in the
RELATIVE KEY clause of the FILE-CONTROL paragraph to access records.
This data item is later described in the WORKING-STORAGE SECTION.
Although the ANSI standard allows only an unsigned integer value for this
data item, it is most efficient if defined as a signed integer of five to
nine digits whose usage should be described as COMPUTATIONAL
SYNCHRONIZED. However, any numeric data item of a sufficient size for the
records in a file can be used.
Relative files opened in sequential mode do not need to use the RELATIVE
KEY data item to access records. You can simply execute input-output
operations on them as though they were sequential files. If, however,
you wish to position the file by using the START statement, you must
specify the RELATIVE KEY data item, since the START statement uses this
data item to find the record you want.
Your program can access a relative file in one of the following ways.
Sequential Access.
Sequential WRITE statements for a relative file release data to the file,
starting with the first record on the file, and proceeding to the second,
third, and so forth in turn. If the RELATIVE KEY data item has been
specified, it is updated each time a sequential WRITE statement is
executed.
NOTE Even though you may already have data on these records, a
sequential WRITE statement will cause the new data to replace it.
Sequential READ statements for relative files start with a particular
record, read it, and proceed to the next existing record.
The record that is read depends upon the type of the last input-output
statement executed before the READ statement is encountered. If a DELETE
or READ statement is executed, the READ statement reads the next existing
record following the record just read or deleted. If an OPEN statement
is executed, the first existing record is read. If a successful START
statement is executed, the record pointed to is read.
Sequential DELETE statements require the use of the READ statement to
position the file to the record to be deleted. This READ statement must
be the last input-output operation performed on the file before the
DELETE statement is encountered.
Sequential REWRITE statements require the use of the READ statement to
position the file to the record to be rewritten. This READ statement
must be the last input-output statement performed on the file before the
REWRITE statement is encountered.
Random Access.
Random access input-output statements use the required RELATIVE KEY data
item to select the record for the READ, WRITE, REWRITE, and DELETE
statements. Thus, to perform a random access input-output operation on a
relative file, you must place the number of the record to be accessed
into the RELATIVE KEY data item before executing the input-output
statement.
When the random access input-output operation is executed, an implicit
seek is performed to find the record, and the specified input-output
operation is performed if possible.
Dynamic Access.
A relative file open in dynamic access mode allows you to access your
file in either random or sequential mode.
The only permissible sequential access that can be performed on a
relative file opened in dynamic access mode is the READ NEXT form of the
READ statement. This statement allows you to access the records of the
file, starting with the record pointed to (if valid) by the current
record pointer. If the record is invalid (that is, has been deleted),
the next valid record in the file is read.
Indexed Files
Indexed files use data items that are integral parts of the records to
control accessing of the records.
For a given indexed file, each record contains a single prime record key,
and zero or more alternate record keys. Each key must be described as
alphanumeric within the record description entry of the associated
indexed file. To indicate which key is the prime record key and which,
if any, are the alternate keys, you must specify their names in the
RECORD KEY and ALTERNATE RECORD KEY clauses, respectively, in the
FILE-CONTROL paragraph of the ENVIRONMENT DIVISION.
The prime record key is used in writing, deleting, or updating records of
an indexed file. Alternate record keys are used only in reading records.
The values of both prime and alternate record keys may be duplicated
within an indexed file. Note however, that since the prime record key
must be used for all input-output operations except reading, you should
be very careful to make sure that if you are deleting a record whose
prime key has duplicates, it is the record you wish to delete, and not
some other record with the same prime record key value.
When a prime record key has duplicates, and you use such a key to access
a record on an indexed file, the file is searched in a chronological
order. That is, the file is searched according to which record was
written first. Thus, the record accessed is the first record containing
the specified record key value that is still active.
As with relative files, an indexed file can be accessed in random,
dynamic, or sequential mode. The actions taken for a specific access
mode and a specific input-output operation are listed below.
Sequential Access.
A sequential access READ statement for an indexed file uses the current
record pointer to read records from the file. The record selected to be
read is determined in essentially the same way as for relative files
opened in sequential mode. See the description of sequential access of
relative files, above.
A sequential access WRITE statement for an indexed file uses the prime
record key to place records in the file. Records must be written in
ascending order according to these keys. Since the prime record key (and
all alternate keys as well) must be alphanumeric, this means that the
records must be written using the ASCII collating sequence to determine
ascending order.
A sequential access REWRITE or DELETE statement requires that a READ
statement be the last executed input-output statement for the file being
referenced.
Random Access.
The READ ... KEY IS form requires that you place a key value into one of
the record key data items (prime or alternate). This data item is then
specified in the READ statement, and the indexed file is searched until a
record having the same value in the same record key is found. This
record is then brought into your program.
A random access WRITE, REWRITE, or DELETE statement uses the contents of
the RECORD KEY data item to select the record to be written or deleted.
Dynamic Access.
When dynamic access is used for indexed files, you can use either the
READ NEXT or the READ...KEY IS form of the READ statement.
The READ NEXT form is used when you wish to read records in a sequential
manner. This statement allows you to access the records of the file
starting with the record pointed to by the current record pointer (if
valid). If the record is invalid, the next valid record in the file is
read.
A dynamic access WRITE, REWRITE, or DELETE statement uses the contents of
the RECORD KEY data item to select the record to be written or deleted.
As an extension to ANSI COBOL'85, HP COBOL II allows the use[REV BEG] of
alphanumeric, computational, numeric[REV END] display (without the
optional SIGN clause) and COMPUTATIONAL-3 data types for RECORD KEY and
ALTERNATE RECORD KEY clauses respectively, in the FILE-CONTROL paragraph
of the ENVIRONMENT DIVISION.
Indexed files are processed with Hewlett-Packard's KSAM subsystem in HP
COBOL II. Refer to the KSAM/XL Reference Manual for information such as
creation and deletion of this file type. Programmatic creation and
deletion of these files can be accomplished by calling the appropriate
intrinsics described in the above manual using the CALL statement.
Opening, closing, and reading of these files is accomplished through the
normal COBOL OPEN, CLOSE, and READ statements.
Sort-Merge Files
The ability to arrange records in a particular order is often required in
COBOL applications. This ability is provided by the sort and merge
features of HP COBOL II.
The sort facility allows you to arrange the records of one or more files
in a specified sequence. The merge facility[REV BEG] merges two or more
previously sorted files.[REV END]
Sort-merge files are the files acted upon by the sort and merge
operations. These files can never be accessed directly, except in input
and output routines associated with a SORT or MERGE statement.
When a SORT or MERGE statement is issued, the records are taken from the
named input file or procedure and then placed in the sort/merge workfile.
Finally, the sorted/merged records are[REV BEG] placed in the output
file, files, or procedure.[REV END]
Refer to Chapter 12 , "SORT-MERGE Operations", for more information on
sort-merge files and operations.
File Status
Every type of file described above[REV BEG] (except sort-merge files)[REV
END] may have a file status data item associated with it. This data item
can be used to check on the success or failure of an input-output
operation involving the file with which it is associated. File status
data items are described in more detail later in this chapter.
Fixed Length Records
Fixed length records must contain the same number of character positions
for all the records in the file. Only one record size can be processed
by all input-output operations on the file. Fixed length records may be
explicitly selected by specifying format 1 of the RECORD clause in the
file description entry for the file, regardless of the individual record
descriptions. Refer to Chapter 7 for more information on the RECORD
clause.
Variable Length Records
Variable length records can contain differing numbers of character
positions among the records on the file. To define variable length
records explicitly, specify the VARYING phrase in the RECORD clause of
the file description entry or the sort-merge file description entry for
the file. The length of a record is either affected by: the data item
referenced in the DEPENDING phrase of the RECORD clause, the DEPENDING
phrase of an OCCURS clause, or the length of the record description entry
for the file.
MPE/iX 5.0 Documentation