File Handling Verbs in COBOL Language

File Handling Verbs in COBOL Language

File handling in COBOL involves using specific file handling verbs to interact with external files. These verbs allow COBOL programs

to perform operations like opening, reading, writing, closing, and deleting files. Here are some of the key file handling verbs in COBOL:

  1. SELECT: The SELECT statement is used to define the structure and attributes of a file. It specifies the file name, the file type (e.g., SEQUENTIAL, INDEXED, RELATIVE), the access mode (e.g., SEQUENTIAL, RANDOM, DYNAMIC), and other file attributes.
   SELECT My-File
      ASSIGN TO "data.dat"
      ORGANIZATION IS SEQUENTIAL
      ACCESS MODE IS RANDOM.
  1. OPEN: The OPEN verb is used to open a file for input, output, or both. It prepares the file for subsequent read or write operations.
   OPEN INPUT My-File
  1. CLOSE: The CLOSE verb is used to close a previously opened file. This ensures that any buffered data is written to the file and that resources are released.
   CLOSE My-File
  1. READ: The READ verb is used to read records from a file. It allows you to retrieve data from the file, one record at a time.
   READ My-File
      AT END
         DISPLAY "End of file."
      NOT AT END
         DISPLAY "Data read from file: " data
   END-READ
  1. WRITE: The WRITE verb is used to write records to a file. It allows you to add or update data in the file.
   WRITE My-File
      FROM record-data
  1. REWRITE: The REWRITE verb is used to update an existing record in an indexed or relative file. It replaces the current record with new data.
   REWRITE My-File
      FROM updated-record-data
  1. DELETE: The DELETE verb is used to remove a record from an indexed or relative file.
   DELETE My-File
  1. START: The START verb is used to position the file’s read or write pointer at a specific location based on a key value. It is commonly used in indexed file processing.
   START My-File
      KEY IS key-value
  1. CLOSE: The CLOSE verb is used to close a previously opened file. This ensures that any buffered data is written to the file and that resources are released.
   CLOSE My-File
  1. REWRITE: The REWRITE verb is used to update an existing record in an indexed or relative file. It replaces the current record with new data. REWRITE My-File FROM updated-record-data
  2. DELETE: The DELETE verb is used to remove a record from an indexed or relative file. DELETE My-File
  3. START: The START verb is used to position the file’s read or write pointer at a specific location based on a key value. It is commonly used in indexed file processing. START My-File KEY IS key-value

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading