Category: File Handling

  • 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

    Open is the first file operation that must be performed. If Open is successful, then only further operations are possible on a file. Only after opening a file, the variables in the file structure are available for processing. FILE STATUS variable is updated after each file operation.

    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 −

    Sr.No.Mode & Description
    1InputInput mode is used for existing files. In this mode, we can only read the file, no other operations are allowed on the file.
    2OutputOutput mode is used to insert records in files. If a sequential file is used and the file is holding some records, then the existing records will be deleted first and then new records will be inserted in the file. It will not happen so in case of an indexed file or a relative file.
    3ExtendExtend mode is used to append records in a sequential file. In this mode, records are inserted at the end. If file access mode is Random or Dynamic, then extend mode cannot be used.
    4I-OInput-Output mode is used to read and rewrite the records of a file.

    Read Verb

    Read verb is used to read the file records. The function of read is to fetch records from a file. At each read verb, only one record can be read into the file structure. To perform a read operation, open the file in INPUT or I-O mode. At each read statement, the file pointer is incremented and hence the successive records are read.

    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.

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO'input.txt'ORGANIZATIONISLINESEQUENTIAL.DATADIVISION.FILESECTION.FD STUDENT.01 STUDENT-FILE.05 STUDENT-ID PIC9(5).05 NAME PICA(25).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).01 WS-EOF PICA(1).PROCEDUREDIVISION.OPENINPUT STUDENT.PERFORMUNTIL WS-EOF='Y'READ STUDENT INTO WS-STUDENT
    
            ATENDMOVE'Y'TO WS-EOF
            NOTATENDDISPLAY WS-STUDENT
         END-READEND-PERFORM.CLOSE STUDENT.STOPRUN.</pre>

    Suppose the input file data available in the input.txt file contains the following −

    20003 Mohtashim M.
    20004 Nishant Malik
    20005 Amitabh Bachhan
    

    When you compile and execute the above program, it produces the following result −

    20003 Mohtashim M.            
    20004 Nishant Malik           
    20005 Amitabh Bachhan 
    

    Syntax

    Following is the syntax to write a record when the file access mode is random −

    READ file-name RECORD INTO ws-file-structure
       KEY IS rec-key
       INVALID KEY DISPLAY 'Invalid Key'
       NOT INVALID KEY DISPLAY 'Record Details: ' ws-file-structure
    END-READ.
    

    Example − The following example reads an existing file using indexed organization. This program can be compiled and executed using JCL on Mainframes where it will display all the records present in the file. On Mainframes server, we do not use text files; instead we use PS files.

    Let's assume that the file present on Mainframes have same content as input.txt file in the above example.

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO IN1
    
      ORGANIZATIONISINDEXEDACCESSISRANDOMRECORDKEYIS STUDENT-ID
      FILESTATUSIS FS.DATADIVISION.FILESECTION.FD STUDENT.01 STUDENT-FILE.05 STUDENT-ID PIC9(5).05 NAME PICA(25).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).PROCEDUREDIVISION.OPENINPUT STUDENT.MOVE20005TO STUDENT-ID.READ STUDENT RECORDINTO WS-STUDENT-FILE
         KEYIS STUDENT-ID
         INVALIDKEYDISPLAY'Invalid Key'NOTINVALIDKEYDISPLAY WS-STUDENT-FILE
      END-READ.CLOSE STUDENT.STOPRUN.</pre>

    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

    Write verb is used to insert records in a file. Once the record is written, it is no longer available in the record buffer. Before inserting records into the file, move the values into the record buffer and then perform write verb.

    Write statement can be used with FROM option to directly write records from the working storage variables. From is an optional clause. If the access mode is sequential, then to write a record, the file must open in Output mode or Extend mode. If the access mode is random or dynamic, then to write a record, the file must open in Output mode or I-O mode.

    Syntax

    Following is the syntax to read a record when the file organization is sequential −

    WRITE record-buffer [FROM ws-file-structure]
    END-WRITE.
    

    Following is the syntax to read a record when the file organization is indexed or relative −

    WRITE record-buffer [FROM ws-file-structure]
       INVALID KEY DISPLAY 'Invalid Key'
       NOT INVALID KEY DISPLAY 'Record Inserted'
    END-WRITE.
    

    Example − The following example shows how to insert a new record in a new file when the organization is sequential.

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO OUT1
    
      ORGANIZATIONISSEQUENTIALACCESSISSEQUENTIALFILESTATUSIS FS.DATADIVISION.FILESECTION.FD STUDENT
    01 STUDENT-FILE.05 STUDENT-ID PIC9(5).05 NAME PICA(25).05CLASSPICX(3).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).05 WS-CLASS PICX(3).PROCEDUREDIVISION.OPENEXTEND STUDENT.MOVE1000TO STUDENT-ID.MOVE'Tim'TO NAME.MOVE'10'TOCLASS.WRITE STUDENT-FILE
      END-WRITE.CLOSE STUDENT.STOPRUN.</pre>

    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)

    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 −

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO IN1
    
      ORGANIZATIONISINDEXEDACCESSISRANDOMRECORDKEYIS STUDENT-ID
      FILESTATUSIS FS.DATADIVISION.FILESECTION.FD STUDENT
    01 STUDENT-FILE.05 STUDENT-ID PIC9(4).05 NAME PICA(12).05CLASSPICX(3).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).05 WS-CLASS PICX(3).PROCEDUREDIVISION.OPENI-O STUDENT.MOVE'1000'TO STUDENT-ID.READ STUDENT
      KEYIS STUDENT-ID
      INVALIDKEYDISPLAYKEYISNOT EXISTING
    END-READ.MOVE'Tim Dumais'TO NAME.REWRITE STUDENT-FILE END-REWRITE.CLOSE STUDENT.STOPRUN.

    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'
    END-DELETE.
    

    Example − to delete an existing record −

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO OUT1
    
      ORGANIZATIONISINDEXEDACCESSISRANDOMRECORDKEYIS STUDENT-ID
      FILESTATUSIS FS.DATADIVISION.FILESECTION.FD STUDENT
    01 STUDENT-FILE.05 STUDENT-ID PIC9(4).05 NAME PICA(12).05CLASSPICX(3).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).05 WS-CLASS PICX(3).PROCEDUREDIVISION.OPENI-O STUDENT.MOVE'1000'TO STUDENT-ID.DELETE STUDENT RECORDINVALIDKEYDISPLAY'Invalid Key'NOTINVALIDKEYDISPLAY'Record Deleted'END-DELETE.CLOSE STUDENT.STOPRUN.

    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 = SHR
    

    When you compile and execute the above program, it produces the following result −

    Record Deleted
    

    Start Verb

    Start verb can be performed only on indexed and relative files. It is used to place the file pointer at a specific record. The access mode must be sequential or dynamic. File must be opened in I-O or Input mode.

    Syntax

    Following is the syntax to place the pointer at a specific record −

    START file-name KEY IS [=, >, <, NOT, <= or >=] rec-key
       INVALID KEY DISPLAY 'Invalid Key'
       NOT INVALID KEY DISPLAY 'File Pointer Updated'
    END-START.
    

    Close Verb

    Close verb is used to close a file. After performing Close operation, the variables in the file structure will not be available for processing. The link between program and file is lost.

    Syntax

    Following is the syntax to close a file −

    CLOSE file-name.
    

  • File Access Mode

    Till now, file organization schemes have been discussed. For each file organization scheme, different access modes can be used. Following are the types of file access modes −

    • Sequential Access
    • Random Access
    • Dynamic Access

    The syntaxes in this module, mentioned along with their respective terms, only refer to their usage in the program. The complete programs using these syntaxes would be discussed in the next chapter.

    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.
    • 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-key1
       ALTERNATE RECORD KEY IS rec-key2
    
    		
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS RELATIVE
       ACCESS MODE IS SEQUENTIAL
       RELATIVE KEY IS rec-key1
    

    Random Access

    When the access mode is RANDOM, the method of record retrieval changes as per the selected file organization.

    • For indexed files, records are accessed according to the value placed in a key field which can be primary or alternate key. There can be one or more alternate indexes.
    • For relative files , records are retrieved through relative record keys.

    Syntax

    Following is the syntax of random access mode −

    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS INDEXED
       ACCESS MODE IS RANDOM
       RECORD KEY IS rec-key1
       ALTERNATE RECORD KEY IS rec-key2
    
    		
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS RELATIVE
       ACCESS MODE IS RANDOM
       RELATIVE KEY IS rec-key1
    

    Dynamic Access

    Dynamic access supports both sequential and random access in the same program. With dynamic access, one file definition is used to perform both sequential and random processing like accessing some records in sequential order and other records by their keys.

    With relative and indexed files, the dynamic access mode allows you to switch back and forth between sequential access mode and random access mode while reading a file by using the NEXT phrase on the READ statement. NEXT and READ functionalities will be discussed in the next chapter.

    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-key1
       ALTERNATE RECORD KEY IS rec-key2
    
    		
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS RELATIVE
       ACCESS MODE IS DYNAMIC
       RELATIVE KEY IS rec-key1

  • File Organization

    File organization indicates how the records are organized in a file. There are different types of organizations for files so as to increase their efficiency of accessing the records. Following are the types of file organization schemes −

    • Sequential file organization
    • Indexed sequential file organization
    • Relative file organization

    The syntaxes in this module, mentioned along with their respective terms, only refer to their usage in the program. The complete programs using these syntaxes would be discussed in the chapter ‘File handling Verbs’.

    Sequential File Organization

    A sequential file consists of records that are stored and accessed in sequential order. Following are the key attributes of sequential file organization −

    • Records can be read in sequential order. For reading the 10th record, all the previous 9 records should be read.
    • Records are written in sequential order. A new record cannot be inserted in between. A new record is always inserted at the end of the file.
    • After placing a record into a sequential file, it is not possible to delete, shorten, or lengthen a record.
    • Order of the records, once inserted, can never be changed.
    • Updation of record is possible. A record can be overwritten, if the new record length is same as the old record length.
    • Sequential output files are good option for printing.

    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.
    • Alternate index can also be created to fetch the records.

    Syntax

    Following is the syntax of indexed sequential file organization −

    INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT file-name ASSIGNTO dd-name-jcl
       ORGANIZATIONISINDEXEDRECORDKEYIS primary-key
       ALTERNATERECORDKEYIS rec-key
    

    Relative File Organization

    A relative file consists of records ordered by their relative address. Following are the key attributes of relative file organization −

    • Records can be read in sequential order just like in sequential and indexed file organization.
    • Records can be accessed using relative key. Relative key represents the records location relative to the address of the start of the file.
    • Records can be inserted using relative key. Relative address is calculated using relative key.
    • Relative file provides the fastest access to the records.
    • The main disadvantage of this file system is that if some intermediate records are missing, they will also occupy space.

    Syntax

    Following is the syntax of relative file organization −

    INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT file-name ASSIGNTO dd-name-jcl
       ORGANIZATIONISRELATIVERELATIVEKEYIS rec-key
    

  • File Handling

    The concept of files in COBOL is different from that in C/C++. While learning the basics of ‘File’ in COBOL, the concepts of both languages should not be corelated. Simple text files cannot be used in COBOL, instead PS (Physical Sequential) and VSAM files are used. PS files will be discussed in this module.

    To understand file handling in COBOL, one must know the basic terms. These terms only serve to understand the fundamentals of file handling. Further in depth terminology would be discussed in the chapter ‘File Handling Verbs’. Following are the basic terms −

    • Field
    • Record
    • Physical Record
    • Logical Record
    • File

    The following example helps in understanding these terms −

    Program Structure

    Field

    Field is used to indicate the data stored about an element. It represents a single element as shown in the above example such as student id, name, marks, total marks, and percentage. The number of characters in any field is known as field size, for example, student name can have 10 characters. Fields can have the following attributes −

    • Primary keys are those fields that are unique to each record and are used to identify a particular record. For example, in students marks file, each student will be having a unique student id which forms the primary key.
    • Secondary keys are unique or non-unique fields that are used to search for related data. For example, in students marks file, full name of student can be used as secondary key when student id is not known.
    • Descriptors fields are used to describe an entity. For example, in students marks file, marks and percentage fields that add meaning to the record are known descriptors.

    Record

    Record is a collection of fields that is used to describe an entity. One or more fields together form a record. For example, in students marks file, student id, name, marks, total marks, and percentage form one record. The cumulative size of all the fields in a record is known as the record size. The records present in a file may be of fixed length or variable length.

    Physical Record

    Physical record is the information that exists on the external device. It is also known as a block.

    Logical Record

    Logical record is the information used by the program. In COBOL programs, only one record can be handled at any point of time and it is called as logical record.

    File

    File is a collection of related records. For example, the students marks file consists of records of all the students.