TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Coding For Files


Steps in File Processing:

File processing in COBOL mainly divided into five steps.

1.DeclarationFile specifications in ENVIRONMENT DIVISION
2. DefinitionRecord specifications in DATA DIVISION.
3. OpenOpen the file in PROCEDURE DIVISION.
4. ProcessPerforming any operations like READ/WRITE/REWRITE/DELETE in PROCEDURE DIVISION.
5. CloseClose the file in PROCEDURE DIVISION.

The following example shows the general format of input/output coding.

Explanations of user-supplied information (bold text in the example) are given below:

IDENTIFICATION DIVISION.
. . .
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT filename ASSIGN TO assignment-name   (1) (2)
    ORGANIZATION IS org  ACCESS MODE IS access   (3) (4)
    FILE STATUS IS file-status   (5)
    . . .
DATA DIVISION.
FILE SECTION.
FD  filename
01  recordname   (6)
    nn . . . fieldlength & type   (7) (8)
    nn . . . fieldlength & type
    . . .
WORKING-STORAGE SECTION.
01  file-status     PIC 99.
    . . .
PROCEDURE DIVISION.
    OPEN iomode filename    (9)
    . . .
    READ filename
    . . .
    WRITE recordname
    . . .
    CLOSE filename
  STOP RUN.

Filename: Any valid COBOL name. You must use the same file-name in the SELECT clause and FD entry, and in the OPEN, READ, START, DELETE, and CLOSE statements.

This name is not necessarily the system file-name. Each file requires its own SELECT clause, FD entry, and input/output statements. For WRITE and REWRITE, you specify a record defined for the file.

Assignment-name: You can code ASSIGN TO assignment-name to specify the target file-system ID and system file-name directly, or you can set the value indirectly by using an environment variable. This name is used in JCL DD name in order to access the file.

If you want to have the system file-name identified at OPEN time, specify ASSIGN USING data-name. The value of data-name at the time of the execution of the OPEN statement for that file is used. You can optionally precede the system file-name by the file-system identifier, using a hyphen as the separator.

The following example shows how inventory-file is dynamically associated with the file /user/inventory/parts by means of a MOVE statement:

SELECT inventory-file ASSIGN USING a-file . . .
. . .
FD inventory-file . . .
. . .
77 a-file PIC X(25) VALUE SPACES.
. . .
    MOVE "/user/inventory/parts" TO a-file
    OPEN INPUT inventory-file

The following example shows how inventory-file is dynamically associated with the indexed Encina SFS file parts, and shows how the alternate index files altpart1 and altpart2 are associated with the fully qualified name (/.:/cics/sfs in this example) of the Encina server.

SELECT inventory-file ASSIGN USING a-file . . .
    ORGANIZATION IS INDEXED
    ACCESS MODE IS DYNAMIC
    RECORD KEY IS FILESYSFILE-KEY
    ALTERNATE RECORD KEY IS ALTKEY1
    ALTERNATE RECORD KEY IS ALTKEY2. . . .
. . .
FILE SECTION.
FD inventory-file . . .
. . .
WORKING-STORAGE SECTION.
01 a-file  PIC X(80). . .
. . .
    MOVE "/.:/cics/sfs/parts(/.:/cics/sfs/parts;altpart1,/.:/
       cics/sfs/parts;altpart2)" TO a-file
    OPEN INPUT inventory-file

Org: Indicates the organization: LINE SEQUENTIAL, SEQUENTIAL, INDEXED, or RELATIVE. If you omit this clause, the default is ORGANIZATION SEQUENTIAL.

Access: Indicates the access mode, SEQUENTIAL, RANDOM, or DYNAMIC. If you omit this clause, the default is ACCESS SEQUENTIAL.

File-status: The COBOL file status key. You can specify the file status key as a two-character alphanumeric or national data item, or as a two-digit zoned decimal or national decimal item.

Recordname: The name of the record used in the WRITE and REWRITE statements. You can specify more than one record for a file.

Fieldlength: The logical length of the field.

Type: Must match the record format of the file. If you break the record description entry beyond the level-01 description, map each element accurately against the record's fields.

IOmode: Specifies the open mode. If you are only reading from a file, code INPUT. If you are only writing to a file, code OUTPUT (to open a new file or write over an existing one) or EXTEND (to add records to the end of the file). If you are doing both, code I-O.

File position indicator:
The file position indicator marks the next record to be accessed for sequential COBOL requests.

You do not explicitly set the file position indicator anywhere in your program. It is set by successful OPEN, START, READ, READ NEXT, and READ PREVIOUS statements. Subsequent READ, READ NEXT, or READ PREVIOUS requests use the established file position indicator location and update it.

The file position indicator is not used or affected by the output statements WRITE, REWRITE, or DELETE. The file position indicator has no meaning for random processing.

Don't worry if you are not clear from this example, We will see detailed explanation with examples in next chapters...

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal