Example: COBOL coding for files

The following example shows the general format of input/output coding. Explanations of user-supplied information (lowercase text in the example) are shown after the code.

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.
(1) 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.
(2) 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.

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
(3) org
Indicates the organization: LINE SEQUENTIAL, SEQUENTIAL, INDEXED, or RELATIVE. If you omit this clause, the default is ORGANIZATION SEQUENTIAL.
(4) access
Indicates the access mode, SEQUENTIAL, RANDOM, or DYNAMIC. If you omit this clause, the default is ACCESS SEQUENTIAL.
(5) 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.
(6) recordname
The name of the record used in the WRITE and REWRITE statements. You can specify more than one record for a file.
(7) fieldlength
The logical length of the field.
(8) 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.
(9) 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.
Restriction: For line-sequential files, I-O is not a valid mode of the OPEN statement.