Read From and Rewrite To the File

Now that an indexed file (idxfile.dat) has been created, you can reuse some of the previous code to perform other file handling operations, by updating the op-code.

  1. In the procedure division, after the last section in the program (just before the end program statement), paste the following code:
               read-all-records section.
               display "Open the file and read all the records".    
               move OP-OPEN-INPUT         to opcode
               move fcd--open-closed      to fcd-open-mode
               perform call-file-handler
               if fcd-status-key-1 not = "0"
                  display "Failed to open file"
                  goback
               end-if
               move 1 to record-key
               move OP-START-EQUAL to opcode
               perform call-file-handler
               move OP-READ-NEXT to opcode
               perform until fcd-status-key-1 not = "0"
                 perform call-file-handler
                 if fcd-status-key-1 = "0"
                    display ex-record(1:fcd-current-rec-len)
                 end-if
               end-perform
               move OP-CLOSE to opcode
               perform call-file-handler
               if fcd-status-key-1 not = "0" 
                  display "Close failed"
                  goback   
               end-if
               .

    When this section is performed, it opens the newly written-to indexed file (op-code OP-OPEN-INPUT), and then reads the records (op-codes OP-START-EQUAL to start at the first key, and OP-READ-NEXT to move on to the next record), displaying them to the screen; after which, the file is closed (op-code OP-CLOSE).

  2. Beneath the previous section, paste the following code:
               rewrite-first-record section.
               move OP-OPEN-I-O to opcode
               perform call-file-handler
               move 1 to record-key
               move OP-START-EQUAL to opcode
               perform call-file-handler
               move OP-READ-NEXT to opcode
               perform call-file-handler
               if fcd-status-key-1 = "0"    
                  move "Fred" to record-data
                  move 9 to fcd-current-rec-len
                  move OP-REWRITE to opcode
                  perform call-file-handler
                  if fcd-status-key-1  = "0"
                   display "Record update : Success"
                  else
                   display "Record update : Failed"
                 end-if
               end-if     
               move OP-CLOSE to opcode
               perform call-file-handler
               if fcd-status-key-1 not = "0" 
                  display "Close failed"
                  goback   
               end-if
               .

    This time the file is opened I-O (op-code OP-OPEN-I-O) and when the first record has been successfully read, it changes its contents (to "Fred") and rewrite the record (op-code OP-REWRITE); after which, the file is closed.

    Now add the two paragraphs that will perform these read and rewrite sections.

  3. In the procedure division, after the paragraph that closes the file (the line that states display "file closed"), paste the following code:
          *> Query the file to retrieve file information 
               move low-values to fcd
               set  fcd-filename-address  to address of ex-filename
               move 80                    to fcd-name-length
               move fcd--determine-org    to fcd-organization
               move fcd--version-number   to fcd-version
               set fcd-filename-address   to address of ex-filename
               set fcd-idxname-address    to address of ex-index-name
               set fcd-key-def-address    to address of ex-keydef
               set fcd-record-address     to address of ex-record
               move OP-QUERY-FILE         to opcode
               perform call-file-handler
               perform display-file-status
               display "file open, ready to read"
               perform read-all-records
               perform rewrite-first-record.
  4. Beneath the previous section, paste the following code:
          *> Now read all the records again
               perform read-all-records
               goback
               stop run. 

    The final perform is to re-read the data file to display the edited record.

  5. Press Ctrl+S to save the changes so far.

Finally, you are ready to compile and run your program.