HP OpenVMS Systems Documentation |
HP COBOL
|
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. |
Creating and processing sequential, line sequential, relative, and indexed files includes the following tasks:
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:
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. |
Creating a sequential or (on Alpha and I64 only) line sequential file involves the following:
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:
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:
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 |
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:
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 |