File organization

You establish the organization of the data when you create a file. Once the file has been created, you can expand the file, but you cannot change the organization.

Sequential organization

The physical order in which the records are placed in the file determines the sequence of records. The relationships among records in the file do not change, except that the file can be extended. Records can be fixed length or variable length; there are no keys.

Each record in the file except the first has a unique predecessor record; and each record except the last has a unique successor record.

Indexed organization

Each record in the file has one or more embedded keys (referred to as key data items); each key is associated with an index. An index provides a logical path to the data records according to the contents of the associated embedded record key data items. Indexed files must be direct-access storage files. Records can be fixed length or variable length.

Each record in an indexed file must have an embedded prime key data item. When records are inserted, updated, or deleted, they are identified solely by the values of their prime keys. Thus, the value in each prime key data item must be unique and must not be changed when the record is updated. You tell COBOL the name of the prime key data item in the RECORD KEY clause of the file-control paragraph.

In addition, each record in an indexed file can contain one or more embedded alternate key data items. Each alternate key provides another means of identifying which record to retrieve. You tell COBOL the name of any alternate key data items on the ALTERNATE RECORD KEY clause of the file-control paragraph.

The key used for any specific input-output request is known as the key of reference.

Relative organization

Think® of the file as a string of record areas, each of which contains a single record. Each record area is identified by a relative record number; the access method stores and retrieves a record based on its relative record number. For example, the first record area is addressed by relative record number 1 and the 10th is addressed by relative record number 10. The physical sequence in which the records were placed in the file has no bearing on the record area in which they are stored, and thus no effect on each record's relative record number. Relative files must be direct-access files. Records can be fixed length or variable length.

Line-sequential organization

In a line-sequential file, each record contains a sequence of characters that ends with a record delimiter. The delimiter is not counted in the length of the record.

When a record is written, any trailing blanks are removed prior to adding the record delimiter. The characters in the record area from the first character up to and including the added record delimiter constitute one record and are written to the file.

When a record is read, characters are read one at a time into the record area until:

  • The first record delimiter is encountered. The record delimiter is discarded and the remainder of the record is filled with spaces.
  • The entire record area is filled with characters. If the first unread character is the record delimiter, it is discarded. Otherwise, the first unread character becomes the first character read by the next READ statement.
  • End-of-file is encountered. The remainder of the record area is filled with spaces.
The record length is determined by one of the following:
  • The requested read length
  • The location of the new-line character (\n)
The only special character in a line sequential file is the new-line character (\n). Any other character including NULL (0x00) is valid and will not cause read or write errors.

Records written to line-sequential files must consist of data items described as USAGE DISPLAY or DISPLAY-1 or a combination of DISPLAY and DISPLAY-1 items. If the CHAR(EBCDIC) compiler option is in effect, a DISPLAY or DISPLAY-1 item can be encoded in either ASCII or EBCDIC, depending on the presence or absence of the NATIVE phrase in the USAGE clause of the data item. A zoned decimal data item either must be unsigned or, if signed, must be declared with the SEPARATE CHARACTER phrase.

A line-sequential file can contain printable characters and control characters. Be aware though that if your file contains a line-feed character (X'0A'), the line-feed character will function as a record delimiter.

The following clauses are not supported for line-sequential files:

  • APPLY WRITE-ONLY clause
  • CODE-SET clause
  • DATA RECORDS clause
  • LABEL RECORDS clause
  • LINAGE clause
  • I-O phrase of the OPEN statement
  • PADDING CHARACTER clause
  • RECORD CONTAINS 0 clause
  • RECORD CONTAINS clause format 2 (for example: RECORD CONTAINS 100 to 200 CHARACTERS)
  • RECORD DELIMITER clause
  • RECORDING MODE clause
  • RERUN clause
  • RESERVE clause
  • REVERSED phrase of the OPEN statement
  • REWRITE statement
  • VALUE OF clause of file description entry
  • WRITE ... AFTER ADVANCING mnemonic-name
  • WRITE ... AT END-OF-PAGE
  • WRITE ... BEFORE ADVANCING

Language elements treated as comments

For other files (sequential, relative, and indexed), the following language elements are syntax checked, but have no effect on the execution of the program:

  • APPLY WRITE-ONLY clause
  • CLOSE ... FOR REMOVAL
  • CLOSE ... WITH NO REWIND
  • CODE-SET clause
  • DATA RECORDS clause
  • LABEL RECORDS clause
  • MULTIPLE FILE TAPE clause
  • OPEN ... REVERSE
  • PADDING CHARACTER clause
  • PASSWORD clause
  • RECORD CONTAINS 0 clause
  • RECORD DELIMITER clause
  • RECORDING MODE clause (for relative and indexed files)
  • RERUN clause
  • RESERVE clause
  • SAME AREA clause
  • SAME SORT AREA clause
  • SAME SORT-MERGE AREA clause
  • VALUE OF clause of file description entry

No error messages are generated (with the exception of the data name option for the LABEL RECORDS, USE ... AFTER ... LABEL PROCEDURE, and GO TO MORE-LABELS clauses).