- Information
- AI Chat
Study Guide - Cobol - File Organization
Computer Engineering (BS CoE)
Ateneo de Manila University
Recommended for you
Preview text
Prepared By: Dominique Joshua B. Ramo COBOL - File Organization
It describes how the information is arranged in a file. To improve the efficiency of accessing the records, files are organized in a variety of ways. The many types of file organization systems are listed below −
- Sequential file organization
- Indexed sequential file organization
- Relative file organization
The syntaxes specified in this module, along with the words associated with them, solely pertain to how they are used in the program. The chapter "File handling Verbs" would cover the full programs employing these syntaxes.
Sequential File Organization
Records are kept and retrieved in a consecutive sequence in a sequential file. These are the main characteristics of sequential file structure −
- Records can be read in a logical sequence. The preceding nine records should all be read before reading the tenth record.
- Records are written in a logical sequence. It is impossible to put a new record in between. The file's end is always where a new record is added.
- A record cannot be deleted, shortened, or lengthened after it has been added to a sequential file.
- Once the records are put, the order cannot be modified.
Prepared By: Dominique Joshua B. Ramo - The record may be updated. If the new record length is the same as the previous record length, a record may be overwritten. - Sequential output files offer a decent printing alternative.. Syntax
Following is the syntax of sequential file organization −
INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name-jcl ORGANIZATION IS SEQUENTIAL
Indexed Sequential File Organization
An indexed sequential file consists of records that can be accessed sequentially. Direct access is also possible. It consists of two parts −
- Data File contains records in sequential scheme.
- Index File contains the primary key and its address in the data file.
Following are the key attributes of sequential file organization −
- Records can be read in sequential order just like in sequential file organization.
- Records can be accessed randomly if the primary key is known. Index file is used to get the address of a record and then the record is fetched from the data file.
- Sorted index is maintained in this file system which relates the key value to the position of the record in the file.
Prepared By: Dominique Joshua B. Ramo
INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name-jcl ORGANIZATION IS RELATIVE RELATIVE KEY IS rec-key
COBOL - File Access Mode
File organization strategies have been covered up to this point. Different access modes can be employed for each file organization scheme. The different file access modes are as follows −
- Sequential Access
- Random Access
- Dynamic Access
The syntaxes specified in this module, along with the words associated with them, solely pertain to how they are used in the program. The following chapter would cover the entire programs employing these syntaxes.
Sequential Access
When the access mode is sequential, the method of record retrieval changes as per the selected file organization.
- For sequential files, records are accessed in the same order in which they were inserted.
- For indexed files, the parameter used to fetch the records are the record key values.
Prepared By: Dominique Joshua B. Ramo - For relative files, relative record keys are used to retrieve the records. Syntax
Following is the syntax of sequential access mode −
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS rec-key ALTERNATE RECORD KEY IS rec-key
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name ORGANIZATION IS RELATIVE ACCESS MODE IS SEQUENTIAL
Prepared By: Dominique Joshua B. Ramo ACCESS MODE IS RANDOM RELATIVE KEY IS rec-key
Dynamic Access
Sequential and random access are both supported by dynamic access within the same software. One file definition is used for both sequential and random processing, such as reading certain entries in sequential order and other records by their keys, when using dynamic access.
Using the NEXT phrase on the READ statement, you may alternate between sequential access mode and random access mode while reading a file in the dynamic access mode for relative and indexed files. The following chapter will go over NEXT and READ functionality.
Syntax
Following is the syntax of dynamic access mode −
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS SEQUENTIAL ACCESS MODE IS DYNAMIC RECORD KEY IS rec-key ALTERNATE RECORD KEY IS rec-key
ENVIRONMENT DIVISION.
Prepared By: Dominique Joshua B. Ramo INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS RELATIVE ACCESS MODE IS DYNAMIC RELATIVE KEY IS rec-key
COBOL - File Handling Verbs
File handling verbs are used to perform various operations on files. Following are the file handling verbs −
- Open
- Read
- Write
- Rewrite
- Delete
- Start
- Close
Open Verb
The first file action that has to be done is open. Only further activities on a file are permitted if Open is successful. The variables in the file structure aren't accessible for processing until a file has been opened. Following each file action, the FILE STATUS variable is updated.
Syntax OPEN "mode" file-name.
Here, file-name is string literal, which you will use to name your file. A file can be opened in the following modes −
Prepared By: Dominique Joshua B. Ramo Read Verb
To read the file records, use the verb read. The read command's purpose is to get records from a file. Only one record may be read into the file structure at a time using the read verb. Open the file in input or output mode in order to execute a read operation. The file pointer is increased with each read command, resulting in the reading of succeeding entries.
Syntax
Following is the syntax to read the records when the file access mode is sequential −
READ file-name NEXT RECORD INTO ws-file-structure AT END DISPLAY 'End of File' NOT AT END DISPLAY 'Record Details:' ws-file-structure END-READ.
Following are the parameters used −
- NEXT RECORD is optional and is specified when an indexed sequential file is being read sequentially.
- INTO clause is optional. ws-file-structure is defined in the WorkingStorage Section to get the values from the READ statement.
- AT END condition becomes True when the end of file is reached.
Example − The following example reads an existing file using line sequential organization. This program can be compiled and executed using Live Demo option where it will display all the records present in the file.
Prepared By: Dominique Joshua B. Ramo
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT ASSIGN TO 'input' ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION. FILE SECTION. FD STUDENT. 01 STUDENT-FILE. 05 STUDENT-ID PIC 9(5). 05 NAME PIC A(25).
WORKING-STORAGE SECTION. 01 WS-STUDENT. 05 WS-STUDENT-ID PIC 9(5). 05 WS-NAME PIC A(25). 01 WS-EOF PIC A(1).
PROCEDURE DIVISION. OPEN INPUT STUDENT. PERFORM UNTIL WS-EOF='Y' READ STUDENT INTO WS-STUDENT AT END MOVE 'Y' TO WS-EOF NOT AT END DISPLAY WS-STUDENT END-READ
Prepared By: Dominique Joshua B. Ramo Let's assume that the file present on Mainframes have same content as input file in the above example.
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT ASSIGN TO IN ORGANIZATION IS INDEXED ACCESS IS RANDOM RECORD KEY IS STUDENT-ID FILE STATUS IS FS.
DATA DIVISION. FILE SECTION. FD STUDENT. 01 STUDENT-FILE. 05 STUDENT-ID PIC 9(5). 05 NAME PIC A(25).
WORKING-STORAGE SECTION. 01 WS-STUDENT. 05 WS-STUDENT-ID PIC 9(5). 05 WS-NAME PIC A(25).
PROCEDURE DIVISION. OPEN INPUT STUDENT.
Prepared By: Dominique Joshua B. Ramo MOVE 20005 TO STUDENT-ID.
READ STUDENT RECORD INTO WS-STUDENT-FILE KEY IS STUDENT-ID INVALID KEY DISPLAY 'Invalid Key' NOT INVALID KEY DISPLAY WS-STUDENT-FILE END-READ.
CLOSE STUDENT. STOP RUN.
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN1 DD DSN = STUDENT-FILE-NAME,DISP=SHR
When you compile and execute the above program, it produces the following result −
20005 Amitabh Bachhan
Write Verb
The verb "write" is used to add records to a file. The record is no longer accessible in the record buffer once it has been written. Move the values into the record buffer before writing records to the file, and then use the write verb.
The write statement can be used in conjunction with the FROM option to write records straight from the active storage variables. The word from is optional. If the access mode is sequential, opening the file in output or extend mode is required
Prepared By: Dominique Joshua B. Ramo DATA DIVISION. FILE SECTION. FD STUDENT 01 STUDENT-FILE. 05 STUDENT-ID PIC 9(5). 05 NAME PIC A(25). 05 CLASS PIC X(3).
WORKING-STORAGE SECTION. 01 WS-STUDENT. 05 WS-STUDENT-ID PIC 9(5). 05 WS-NAME PIC A(25). 05 WS-CLASS PIC X(3).
PROCEDURE DIVISION. OPEN EXTEND STUDENT. MOVE 1000 TO STUDENT-ID. MOVE 'Tim' TO NAME. MOVE '10' TO CLASS. WRITE STUDENT-FILE END-WRITE. CLOSE STUDENT. STOP RUN.
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //OUT1 DD DSN = OUTPUT-FILE-NAME,DISP = (NEW,CATALOG,DELETE)
Prepared By: Dominique Joshua B. Ramo When you compile and execute the above program, it will add a new record to the output file.
1000 Tim 10
Rewrite Verb
Rewrite verb is used to update the records. File should be opened in I-O mode for rewrite operations. It can be used only after a successful Read operation. Rewrite verb overwrites the last record read.
Syntax
Following is the syntax to read a record when the file organization is sequential −
REWRITE record-buffer [FROM ws-file-structure] END-REWRITE.
Following is the syntax to read a record when the file organization is indexed or relative −
REWRITE record-buffer [FROM ws-file-structure] INVALID KEY DISPLAY 'Invalid Key' NOT INVALID KEY DISPLAY 'Record Updated' END-REWRITE.
Example − The following example shows how to update an existing record which we have inserted in the previous Write step −
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
Prepared By: Dominique Joshua B. Ramo END-READ.
MOVE 'Tim Dumais' TO NAME. REWRITE STUDENT-FILE END-REWRITE. CLOSE STUDENT. STOP RUN.
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN1 DD DSN = OUTPUT-FILE-NAME,DISP = SHR
When you compile and execute the above program, it will update the record −
1000 Tim Dumais 10
Delete Verb
Delete verb can be performed only on indexed and relative files. The file must be opened in I-O mode. In sequential file organization, records cannot be deleted. The record last read by the Read statement is deleted in case of sequential access mode. In random access mode, specify the record key and then perform the Delete operation.
Syntax
Following is the syntax to delete a record −
DELETE file-name RECORD INVALID KEY DISPLAY 'Invalid Key' NOT INVALID KEY DISPLAY 'Record Deleted'
Prepared By: Dominique Joshua B. Ramo END-DELETE.
Example − to delete an existing record −
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT ASSIGN TO OUT ORGANIZATION IS INDEXED ACCESS IS RANDOM RECORD KEY IS STUDENT-ID FILE STATUS IS FS.
DATA DIVISION. FILE SECTION. FD STUDENT 01 STUDENT-FILE. 05 STUDENT-ID PIC 9(4). 05 NAME PIC A(12). 05 CLASS PIC X(3). WORKING-STORAGE SECTION. 01 WS-STUDENT. 05 WS-STUDENT-ID PIC 9(5). 05 WS-NAME PIC A(25). 05 WS-CLASS PIC X(3).
PROCEDURE DIVISION.
Study Guide - Cobol - File Organization
Course: Computer Engineering (BS CoE)
University: Ateneo de Manila University
- Discover more from: