Rational Developer for System z
COBOL for Windows, Version 7.5, Programming Guide


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) follow 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 actual name of the file as known to the system. 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 the file-name as it is known to the system directly, or you can set a 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 identification by the file-system type identification.

The following example illustrates how inventory-file is dynamically (by way of a MOVE statement) associated with the file d:\inventory\parts.

SELECT inventory-file ASSIGN USING a-file . . .
. . .
FD inventory-file . . .
. . .
77 a-file PIC X(20) VALUE SPACES.
. . .
    MOVE “d:\inventory\parts” 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: I-O is not a valid parameter of OPEN for line-sequential files.


Terms of use | Feedback

Copyright IBM Corporation 1996, 2008.
This information center is powered by Eclipse technology. (http://www.eclipse.org)