Input-Output Section

144 Views


The INPUT-OUTPUT SECTION is a section of the program where you can define file-handling statements. These statements are used to read from or write to external files, such as data files or report files.

The INPUT-OUTPUT SECTION follows the FILE SECTION and comes before the PROCEDURE DIVISION in a COBOL program. It is optional, and you only need to include it if you are using file handling statements in your program.

The INPUT-OUTPUT SECTION specifies the file information that is useful to access the file in the program. It should start in Area-A, followed by a period.

[INPUT-OUTPUT SECTION.
[FILE-CONTROL paragraph]
[I-O-CONTROL paragraph]]

 

File Categories

The IBM i system has four categories of files: database files, device files, DDM files, and save files.

This manual uses the term file to mean any of these files.

Database Files

Database files allow information to be permanently stored on the system. A database file is subdivided into groups of records called members. There are two types of database files: physical files and logical files.

A physical file is a file that contains data records (similar to disk files on other systems).

A logical file is a database file through which data from one or more physical files can be accessed. The format and organization of this data is different from that of the data in the physical file(s). Each logical file can define a different access path (index) for the data in the physical file(s), and can exclude and reorder the fields defined in the physical file(s).

Distributed Files

Distributed files allow a database file to be spread across multiple System i5/OS servers, while retaining the look and capability of a single database.

Device Files

A device file reads from or writes to a device or remote system. It controls the transfer of data between the physical device or remote system and the program.

DDM Files
Distributed Data Management (DDM) allows you to access data that reside on remote systems that support DDM. You can retrieve, add, update, or delete data records in a file that resides on another system.

Save Files

A save file is a file that is used to prepare data in a format that is correct for backup and recovery purposes or for transportation to another system.

It contains the output that is produced from the Save Library (SAVLIB) or Save Object (SAVOBJ) CL commands.

 

Paragraphs

The Input-Output section of the Environment Division contains two paragraphs:

  • FILE-CONTROL paragraph
  • I-O-CONTROL paragraph.

FILE-CONTROL paragraph

Names and associates the files with the external media. The keyword FILE-CONTROL may appear only once, at the beginning of the FILE-CONTROL paragraph. It must begin in Area A, and be followed by a separator period.

I-O-CONTROL paragraph

Specifies information needed for transmission of data between external media and the COBOL program.

 

FILE-CONTROL

Paragraph The FILE-CONTROL paragraph associates each file in the COBOL program with an external medium, and specifies file organization, access mode, and other information.

COBOL allows for four distinct kinds of file input and output:

  • Sequential
  • Relative
  • Indexed

FILE-CONTROL Paragraph 


FILE-CONTROL paragraph contains complete information about the files used in the program. It maps the logical file (used in program) to the corresponding physical file which is external to the program.

[ENVIRONMENT DIVISION.]
[INPUT-OUTPUT SECTION.]
[FILE-CONTROL.]
    [SELECT [OPTIONAL] logical-file-name ASSIGN TO DSNname]
    [ORGANIZATION IS file-organization]
    [ACCESS MODE IS access-mode]
	[RECORD KEY IS record-key]
	[ALTERNATE RECORD KEY IS alt-record-key WITH DUPLICATES]
	[RELATIVE KEY IS relative-key]
	[RECORD DELIMITER IS record-delimiter]
    [FILE STATUS IS file-status].
  • FILE-CONTROL paragraph entry should begin in Area-A. All other entries in the FILE-CONTROL paragraph should start in Area-B.
  • If no files are used in the program, we can ignore the FILE-CONTROL paragraph and all its entries.

SELECT Clause -


SELECT is used to map the logical-file-name with the physical file external to the program.

OPTIONAL

It specifies that the input file is not necessarily available during the execution of the program. Control transfers to the AT END phrase when the first file READ is executed on that file.

logical-file-name -

This is the name we use in the program to refer to the file. It's like an alias for the physical file. (e.g., FILE1, FILE2). It should be unique within this program and same as an FD or SD entry in the DATA DIVISION.

DSNname -

This is the 8-character device name where the file is stored. i.e., DDname in JCL or DSName in CICS. (e.g., DISK, TAPE).

ORGANIZATION clause -


It specifies the file organization to the program, and the file organizations are - SEQUENTIAL, INDEXED, and RELATIVE. If the ORGANIZATION is not coded, assumes the file is sequential. ORGANIZATION is needed for sequential files and not needed for inded and relative files.

The most frequently used file organizations are -

  • SEQUENTIAL - Specifies the file is sequential.
  • INDEXED - Specifies the file is indexed.
  • RELATIVE - Specifies the file is relative.

ACCESS MODE clause -


It defines how the file records are accessed. If it is not coded, sequential access is assumed. ACCESS MODE supports three types of accessing modes -

  • SEQUENTIAL - The records will be accessed sequentially and applicable to sequential, indexed, and relative files.
  • RANDOM - The records will be accessed randomly (in a programmer-specified manner) and applicable to indexed and relative files.
  • DYNAMIC - It is a combination of sequential, random, or both access modes and is applicable to indexed and relative files.

RECORD KEY clause -


The RECORD KEY clause names a key field that provides a unique way to access the record of an indexed file. The RECORD KEY clause is applicable only for indexed files.

record-key - record-key is a variable that contains the key value of the record to access or process it.

ALTERNATE RECORD KEY clause -


ALTERNATE RECORD KEY clause names the alternative key that provides an alternative way to access the record. It is applicable only to indexed files.

alt-key - It specifies the alternative key field and should be part of the record associated with the file.

WITH DUPLICATES - It specifies the file can have duplicates in ALTERNATE KEY field.

RELATIVE KEY clause -


The RELATIVE KEY clause names the relative record number (RRN) to access the records of a relative file.

rel-key -

  • rel-key represents the RRN value.
  • rel-key is not part of the record and should be defined as an unsigned integer variable in the program.
  • rel-key doesn't require for SEQUENTIAL access.
  • rel-key is always required for RANDOM and DYNAMIC accessing.

FILE STATUS clause -


The FILE STATUS clause allows naming a variable used to get the success or failure status of the input-output operations performed on the file.

When the FILE STATUS clause is coded, the system moves a value into the file status variable after each input-output operation performed on the file.

  • file-status (non-vsam files) - It is a two-character alphanumeric variable.
  • file-status (VSAM files - ESDS, KSDS, RRDS) - It is an alphanumeric group variable of 6 bytes.

Post Comments