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 andFD
entry, and in theOPEN
,READ
,START
,DELETE
, andCLOSE
statements. This name is not necessarily the system file-name. Each file requires its ownSELECT
clause,FD
entry, and input/output statements. ForWRITE
andREWRITE
, 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, specifyASSIGN USING
data-name. The value of data-name at the time of the execution of theOPEN
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 aMOVE
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 CICS® 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 CICS 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
, orRELATIVE
. If you omit this clause, the default isORGANIZATION SEQUENTIAL
. - (4) access
- Indicates the access mode,
SEQUENTIAL
,RANDOM
, orDYNAMIC
. If you omit this clause, the default isACCESS 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
andREWRITE
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, codeOUTPUT
(to open a new file or write over an existing one) orEXTEND
(to add records to the end of the file). If you are doing both, codeI-O
.Restriction: For line-sequential files,I-O
is not a valid mode of theOPEN
statement.