File Organization

7.3 File Organization

There are at least four ways in which the records on a file may be organised: SEQUENTIAL, LINE SEQUENTIAL, RELATIVE, AND INDEXED. When a file contains several records (hundreds or even thousands) if you only wanted to access one or two of them, it would waste processor time having to search an entire file in order to read them if stored in sequential or line sequential formats. Hence, relative and indexed files are of particular advantage.  

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Relative files

These files are organised so that a record can be accessed by referring to its position within the file, i.e. relative to other records. This is acheived by calculating the size (in characters, defined in the FD description) of each record and multiplying it by the required nth record....eh?? you ask. Consider the following program:



  • In the environment division, the assign clause contains a number of extra words. The organization is RELATIVE. This is followed by ACCESS MODE IS DYNAMIC. This means that the file can be read sequentially or RANDOMLY, i.e. direct access whereby the computer can calculate where to look for the required record. You alternatively use ACCESS MODE IS RANDOM but this doesn't allow for a sequential access option (so what's the point using it...?).
  • The next line RELATIVE KEY IS ENTER-NUM refers to this item defined in working storage that will contain the record number required. When the number is entered into the keyboard (ACCEPT ENTER-NUM), the computer will multiply this number (minus 1) by the size of the record (CUSTOMER-DETAILS containing 26 characters):
    e.g.
       (102-1) * 26 = 2626 characters into the file will be immediately followed by the 102nd record.
  • The read statement, rather than using AT END and NOT AT END, uses INVALID KEY and NOT INVALID KEY. Here these depend on whether the file has been found or not.
  • The PIC size of ENTER-NUM is 9(4), which limits the file to 9,999 records but you could increase this if you wanted.
  • It should be noted that you are not allowed to use an item defined in the FD as a relative key.
 

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Indexed files

An indexed file contains records that, unlike relative files, do not require the key to be numeric. Look at the following code (similar to the above code):



The main differences between this example and the relative file example are that (1) the term RECORD KEY is used, rather than RELATIVE KEY; (2) any field can be used from the record. However, the field must be unique otherwise a duplicate key error would occur. Rather than directly access the file, as in relative file access, the computer searches a separate index file that contains pointers to the position of the actual record on the indexed (data) file. The field in the index, whether numeric or alphanumeric, must be in strict ascending order (ASCII characters are ordered according to their ASCII value, e.g. A  

In order to read the indexed file sequentially, but in ascending order on the key field, the verb START is used. For the above example:


OPEN INPUT IN-FILE START IN-FILE KEY GREATER THAN 'D23301' INVALID KEY DISPLAY 'NO MORE RECORDS BEYOND THIS POINT' NOT INVALID KEY ....statements to process rest of file e.g. READ within a loop END-START

To differentiate between a sequential READ and a random READ when using DYNAMIC access mode, you would use the statements READ (with INVALID KEY) for random read, and READ...NEXT for sequential read (with AT END) e.g. :


*Random read MOVE 'E11323' TO CODE-NUM READ IN-FILE INVALID KEY DISPLAY 'CODE NOT FOUND' END-READ *Sequential read READ IN-FILE NEXT AT END MOVE 'Y' TO EOF-FLAG END-READ