[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

Sample SELECT statements for line sequential files with sequential access modes are shown in Example 6-20.

Example 6-20 SELECT Statements for Line Sequential Files with Sequential Access Modes (Alpha, I64)

          (1)                                          (2)
FILE-CONTROL.                                FILE-CONTROL.
    SELECT MAMMALS                               SELECT VACATION-SPOTS
           ASSIGN TO "DOLPHINS"                         ASSIGN TO "BAHAMAS"
           ORGANIZATION IS LINE SEQUENTIAL              ORGANIZATION IS LINE SEQUENTIAL.
           ACCESS MODE IS SEQUENTIAL.

6.3 Creating and Processing Files

Creating and processing sequential, line sequential, relative, and indexed files includes the following tasks:

  1. Opening the file
  2. Executing valid I/O statements
  3. Closing the file

Sections 6.3.2, 6.3.3, and 6.3.4 describe the specific tasks involved in creating and processing sequential, relative, and indexed files.

6.3.1 Opening and Closing Files

An HP COBOL program must open a file with an OPEN statement before any other I/O or Report Writer statement can reference it. Files can be opened more than once in the same program as long as they are closed before being reopened.

Sample OPEN and CLOSE statements are shown in Example 6-21.

Example 6-21 OPEN and CLOSE Statements

   .
   .
   .
OPEN INPUT MASTER-FILE.
OPEN OUTPUT REPORT-FILE.
OPEN I-O   MASTER-FILE2
           TRANS-FILE
     OUTPUT REPORT-FILE2.
CLOSE MASTER-FILE.
CLOSE TRANS-FILE, MASTER-FILE2
      REPORT-FILE, REPORT-FILE2.
   .
   .
   .

The OPEN statement must specify one of the following four open modes:

INPUT
OUTPUT
I-O {Not for LINE SEQUENTIAL}
EXTEND

Your choice, along with the file's organization and access mode, determines which I/O statements you can use. Sections 6.3.2, 6.3.3, and 6.3.4 discuss the I/O statements for sequential, relative, and indexed files, respectively. Section 12.8.4, Case Sensitivity on Tru64 UNIX explains the importance of attention to case.

When your program performs an OPEN statement, the following events take place:

  1. The I/O system builds a file specification by using the contents of the VALUE OF ID clause, if any, to alter or complete the file specification in the ASSIGN clause. Logicals and environment variables are translated.
  2. The I/O system checks the file's current status. If the file is unavailable, or if it was closed WITH LOCK, the OPEN statement fails. (See Chapter 8 for information on file sharing.)
  3. If the file specification names an invalid device, or contains any other errors, the I/O system generates an error message and the OPEN statement fails.
  4. The I/O system takes one of the following actions if it cannot find the file:
    1. If the file's OPEN mode is OUTPUT, the file is created.
    2. If the file's OPEN mode is EXTEND, or I-O, the OPEN statement fails, unless the file's SELECT clause includes the OPTIONAL phrase. If the file's SELECT clause includes the OPTIONAL phrase, the file is created.
    3. If the file's OPEN mode is INPUT, and its SELECT clause includes the OPTIONAL phrase, the OPEN statement is successful. The first read on that file causes the AT END or INVALID KEY condition.
    4. If none of the previous conditions is met, the OPEN fails and the Declarative USE procedure (if any) gains control. If no Declarative USE procedure exists, the I/O system aborts the program.
  5. If the file's OPEN mode is OUTPUT, and a file by the same name already exists, a new version is created.
  6. If the file characteristics specified by the program attempting an OPEN operation differ from the characteristics specified when the file was created, the OPEN statement fails.

If the file is on magnetic tape, the I/O system rewinds the tape. (To close a file on tape without rewinding the tape, use the NO REWIND phrase.) This speeds processing when you want to write another file beyond the end of the first file, as in the following example:


CLOSE MASTER-FILE NO REWIND.

You can also close a file and prevent your program from opening that file again in the same run, as in the following example:


CLOSE MASTER-FILE WITH LOCK.

6.3.2 File Handling for Sequential and Line Sequential (Alpha, I64) Files

Creating a sequential or (on Alpha and I64 only) line sequential file involves the following:

  1. Opening the file for OUTPUT or EXTEND
  2. Executing valid I/O statements
  3. Closing the file

By default, HP COBOL assumes sequential organization and sequential access mode. (See Example 6-22.)

Example 6-22 Creating a Sequential File

IDENTIFICATION DIVISION.
PROGRAM-ID. SEQ01.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT TRANS-FILE ASSIGN TO "TRANS.DAT".
DATA DIVISION.
FILE SECTION.
FD  TRANS-FILE.
01  TRANSACTION-RECORD    PIC X(25).
PROCEDURE DIVISION.
A000-BEGIN.
    OPEN OUTPUT TRANS-FILE.
    PERFORM A010-PROCESS-TRANS
       UNTIL TRANSACTION-RECORD = "END".
    CLOSE TRANS-FILE.
    STOP RUN.
A010-PROCESS-TRANS.
    DISPLAY "Enter next record  - X(25)".
    DISPLAY "enter END to terminate the session".
    DISPLAY "-------------------------".
    ACCEPT TRANSACTION-RECORD.
    IF TRANSACTION-RECORD NOT = "END"
       WRITE TRANSACTION-RECORD.

Example 6-23 Creating a Line Sequential File (Alpha, I64)

        IDENTIFICATION DIVISION.
        PROGRAM-ID. LINESEQ01.
        ENVIRONMENT DIVISION.
        INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT LINESEQ-FILE ASSIGN TO "LINESEQ.DAT".
        DATA DIVISION.
        FILE SECTION.
        FD  LINESEQ-FILE.
        01  LINESEQ-RECORD    PIC X(25).

        PROCEDURE DIVISION.
        A000-BEGIN.
            OPEN OUTPUT LINESEQ-FILE.
            CLOSE LINESEQ-FILE.
            STOP RUN.

By default, HP COBOL assumes sequential access mode when the line sequential organization is specified. (See Example 6-23.) <>

Statements for Sequential and Line Sequential (Alpha, I64) File Processing

Processing a sequential file or line sequential file (Alpha, I64) involves the following:

  1. Opening the file
  2. Processing the file with valid I/O statements
  3. Closing the file

Table 6-3 lists the valid I/O statements for sequential files, and Table 6-4 lists the valid I/O statements for line sequential files. Both tables illustrate the following relationships:

  • Organization determines valid access modes.
  • Organization and access mode determine valid open modes.
  • All three (organization, access, and open mode) enable or disable I/O statements.

Table 6-3 Valid I/O Statements for Sequential Files
      Open Mode
File
Organization
Access
Mode
Statement INPUT OUTPUT I/O EXTEND
SEQUENTIAL SEQUENTIAL READ Yes No Yes No
    REWRITE No No Yes No
    WRITE No Yes No Yes
    UNLOCK Yes Yes Yes Yes

Writing a Sequential File

Each WRITE statement appends a logical record to the end of an output file, thereby creating an entirely new record in the file. The WRITE statement appends records to files that are OPEN for the following modes:

  • OUTPUT---Output mode can create the following two kinds of files:
    • Storage files---A storage file remains on tape or disk for future reference or processing.
    • Print-control files---The Data Division LINAGE clause, the Environment Division APPLY PRINT-CONTROL clause, the Procedure Division ADVANCING phrase (in the WRITE statement), or Report Writer statements and phrases designates a file as a print-control file.
      On OpenVMS Alpha and OpenVMS I64, each record in a print-control file contains a header that performs line spacing. On Tru64 UNIX, line spacing is done with blank records in print-control files.
  • EXTEND---Extend mode permits new records to be added in sequence after the last record of an existing file (see Extending a Sequential File or Line Sequential File (Alpha, I64) in Section 6.5.1).

Table 6-4 Valid I/O Statements for Line Sequential Files (Alpha, I64)
      Open Mode
File
Organization
Access
Mode
Statement INPUT OUTPUT EXTEND
LINE
SEQUENTIAL
SEQUENTIAL READ Yes No No
    WRITE No Yes Yes
    UNLOCK Yes Yes Yes


Previous Next Contents Index