This chapter explains the available COBOL file organizations and gives examples of using them.
Using only COBOL syntax, COBOL programs can create, update and read files of four different organizations:
Line Sequential files are a special type of sequential file. They correspond to simple text files as produced by the standard editor provided with your operating system.
Sequential files are the simplest form of COBOL file. Records are placed in the file in the order they are written, and can only be read back in the same order.
Every record in a relative file can be accessed directly without having to read through any other records. Each record is identified by a unique ordinal number both when it is written and when it is read back.
Indexed files are the most complex form of COBOL file which can be handled directly by COBOL syntax. Records in an indexed file are identified by a unique user-defined key when written. Each record can contain any number of user-defined keys which can be used to read the record, either directly or in key sequence.
A sequential file is a file in which the records can only be accessed sequentially. Records are always added to the end of the file.
In line sequential files, each record in the file is separated from the next by a record delimiter. On DOS, Windows and OS/2 this is a carriage return (x"0D") and a line feed (x"0A") character. On UNIX it is just the line feed (x"0A") character. These characters are inserted after the last non-space character in each record so line sequential files always contain variable-length records.
Report files are line sequential, since most PC printers require the carriage return and/or line feed characters at the end of each record.
Most PC editors produce line sequential files, and these files can therefore be edited with almost any PC editor.
The primary use of line sequential files is for display-only data.
Caution: We recommend that you do not use line sequential files for binary or packed data fields.
Line sequential files are also known as text files, or flat ASCII files.
When you declare a file as line sequential in COBOL, you do so through the SELECT clause.
Example
Creating a line sequential file:
file-control. select lineseq assign to "line.dat" organization is line sequential. file section. fd lineseq record contains 80 characters. 01 lineseq-fd-record pic x(80).
Record sequential files are nearly always simply called sequential files, since record sequential is the default for a sequential file.
Records in a record sequential file can be either fixed or variable in length.
Variable-length records save disk space. There are many applications that can benefit from the use of variable-length records. A common example is where your application generates many small records, with occasional large ones. If you make the record length as long as the largest record, you waste a lot of disk space. The way to prevent this waste is to use variable-length records.
When you declare a file as record sequential in COBOL, you do so through the SELECT clause.
Example
Creating a record sequential file with fixed-length records.
file-control. select recseq assign to "recseq.dat" organization is record sequential.
file section. fd recseq record contains 80 characters. 01 recseq-fd-record pic x(80).
Note: In place of the ORGANIZATION clause above, you could use:
organization is sequential
Or, you could simply omit the ORGANIZATION clause, as record sequential is the default file organization (if the SEQUENTIAL directive is not set).
Example
Creating a record sequential file with variable-length records.
file control. select recseqv assign to "recseqv.dat" organization is sequential. file section. fd recseqv recording mode is v record varying from 3 to 80 characters. 01 recseqv-fd-record pic x occurs 3 to 80 times depending on ws-record-length. working-storage section. 01 ws-record-length pic 99.
With relative file organization, you can access records sequentially or randomly. For sequential access, you simply do a sequential READ to get the next record in the file. For random access, you specify the ordinal number of the record in the file.
Relative files have a fixed-length file format. You can declare that you want the records to have a recording mode of "variable" but even if you do this, the system assumes the maximum record length for all WRITE statements to the file, and pads the unused character positions. So, when you are in a situation where you have a lot to gain by using variable-length records, you should avoid relative files because they are always fixed format.
Relative files have the fastest access time of all the file types used by this COBOL system so, if speed of access is the most important consideration, you should consider using relative files.
With relative files, you can have numeric keys, but you cannot key on fields. If you need to access data randomly based on certain fields, you must use indexed files.
Example
Creating a relative file with a record length of 80 characters:
file-control. select relfil assign to "relfil.dat" organization is relative access mode is random relative key is relfil-key. file section. fd relfil record contains 80 characters. 01 recseq-fd-record pic x(80). working-storage section. 01 relfil-key pic 9(8) comp-x.
Note: The relative key field is relfil-key
. When you are randomly
accessing this file, there is no KEY IS field on the READ statement. The number
in relfil-key
determines which record is read. (For sequential access, a simple
READ statement gets the next record.)
Indexed file access enables you to access records either randomly or sequentially, using one or more key fields in the individual records.
Key comparisons are made on a byte-by-byte basis from right to left using the ASCII collating sequence.
COBOL indexed files are actually made up of two physical files: a data file and an index file. The index file is created automatically, and has an extension of .idx; the data file can have any other extension, although .dat is very common.
Records in indexed files can be either fixed or variable in length.
Whenever you need to provide users with many different views of a file, you need indexed files. In your programs, this implies the need for random access, keyed on one or more fields in the records.
Creating an indexed file with fixed-length 80-byte records keyed on the first five bytes of each record:
file-control. select isamfil assign to "isamfil.dat" organization is indexed access mode is dynamic record key is isamfil-fd-key. file section. fd isamfil record contains 80 characters. 01 isamfil-fd-record. 05 isamfil-fd-key pic x(5). 05 isamfil-fd-data pic x(75).
Creating an indexed file with variable-length records, varying in length from 5 to 80 bytes:
file-control. select isamvar assign to "isamvar.dat" organization is indexed access mode is dynamic record key is isamvar-fd-key.
file section. fd isamvar. record is varying in size from 5 to 80 characters depending on ws-record-count. 01 isamvar-fd-record. 05 isamvar-fd-key pic x(5). 05 isamvar-fd-data pic x(75). working-storage section. 01 ws-record-count pic 99 comp-x.
Note: The keys defined for the file must all lie in the fixed part of the record.
When using COBOL, you can open data files in one program, and perform file operations (such as READ and WRITE) in another, as long as all the programs are in the same run unit. This is called using external files. There are several reasons for using external files:
To use external files, you must:
Example
The following programs illustrate how to use external files in COBOL programs. The main program opens a file, which is written to, and read from, in two separate subprograms before the file is closed at the end of the main program. For the sake of brevity, no file status checking is performed.
The programs are included in your cobol\demo directory, if you want to compile and run them yourself. They are in a file named extfile.cbl. The subprograms are also part of this file - they are named readfile and writefil.
You can animate these programs to familiarize yourself with the way external files work. The source listing that follows is the one you get if you compile them. To compile and animate or run the sample program, use the commands:
DOS, Windows and OS/2:
cobol extfile anim nognt; animate extfile or: run extfile
UNIX:
cob -a extfile.cbl cobrun extfile
If you run this suite of programs more than once, delete isamfil.dat and isamfil.dat.idx after each run.
Here is the source for the extfile.cbl program (also containing the readfile and writefil subprograms):
1$set ans85 mf noosvs 2***************************************************** 3* 4* Copyright Micro Focus International Ltd. 5* 6* EXTFILE.CBL 7* 8* This program demonstrates how to use EXTERNAL files 9* It calls WRITEFIL to write some records to a data 10* file and READFILE to read the same records back 11* (without opening or closing the file between calls 12* READFILE displays the output. 13* 14**************************************************** 15 identification division. 16 program-id. extfile. 17 environment division. 18 input-output section. 19 file-control. 20 select finfile assign to "isamfil.dat" 21 organization is indexed 22 record key is fd-tran-date 23 access mode is dynamic. 24 25 file section. 26 fd finfile 27 is external 28 record contains 50 characters. 29 01 fd-finfile-record. 30 05 fd-tran-date pic x(4). 31 05 fd-with-or-dep pic x(2). 32 05 fd-amount pic 9(5)v99. 33 34 procedure division. 35 main-line. 36 perform open-file 37 perform write-to-the-file 38 perform start-file 39 perform read-the-file 40 perform close-file 41 stop run. 42 43 open-file. 44 open i-o finfile. 45 46 start-file. 47 move 1111 to fd-tran-date 48 start finfile key = fd-tran-date. 49 50 write-to-the-file. 51 call "writefil". 52 53 read-the-file. 54 call "readfile". 55 56 close-file. 57 close finfile. 58 end program extfile. 59***************************************************** 60 identification division. 61 program-id. readfile. 62 environment division. 63 input-output section. 64 file-control. 65 select finfile assign to "isamfil.dat" 66 organization is indexed 67 record key is fd-tran-date 68 access mode is dynamic. 69 70 file section. 71 fd finfile 72 is external 73 record contains 50 characters. 74 01 fd-finfile-record. 75 05 fd-tran-date pic x(4). 76 05 fd-with-or-dep pic x(2). 77 05 fd-amount pic 9(5)v99. 78 79 working-storage section. 80 01 ws-end-of-file pic 9 value 0. 81 01 ws-subtotal pic s9(5)v99 value 0. 82 01 ws-total pic -(4)9.99. 83 84 procedure division. 85 main-line. 86 perform read-the-file. 87 perform until ws-end-of-file = 1 88 perform calculate-totals 89 perform read-the-file 90 end-perform. 91 perform display-output. 92 exit program. 93 stop run. 94 95 read-the-file. 96 read finfile next record at end 97 move 1 to ws-end-of-file. 98 99 calculate-totals. 100 evaluate fd-with-or-dep 101 when "WI" 102 subtract fd-amount from ws-subtotal 103 when "DE" 104 add fd-amount to ws-subtotal 105 end-evaluate. 106 107 display-output. 108 move ws-subtotal to ws-total 109 display "account balance = ", ws-total. 110 111 end program readfile. 112**************************************************** 113 identification division. 114 program-id. writefil. 115 environment division. 116 input-output section. 117 file-control. 118 select finfile assign to "isamfil.dat" 119 organization is indexed 120 record key is fd-tran-date 121 access mode is dynamic. 122 123 file section. 124 fd finfile 125 is external 126 record contains 50 characters. 127 01 fd-finfile-record. 128 05 fd-tran-date pic x(4). 129 05 fd-with-or-dep pic x(2). 130 05 fd-amount pic 9(5)v99. 131 132 procedure division. 133 main-line. 134 perform write-records 135 exit program 136 stop run. 137 138 write-records. 139 140* write a WIthdrawal record 141 move 1111 to fd-tran-date. 142 move 'WI' to fd-with-or-dep. 143 move 23.55 to fd-amount. 144 write fd-finfile-record. 145 146* write a DEposit record 147 move 2222 to fd-tran-date. 148 move 'DE' to fd-with-or-dep. 149 move 123.55 to fd-amount. 150 write fd-finfile-record. 151 152 end program writefil.
In extfile, note the SELECT clause (starting on line 20) and the FD clause (starting on line 26). For other programs to access file isamfil.dat, these two clauses must be present, and described identically as they are here. Typically, a copyfile is used for the SELECT and FD clauses. Here, they are simply duplicated in the readfile and writefil subprograms.
In the main-line
paragraph (line 35), you see the simple operations
performed by extfile: the file is opened, written to, read, and closed.
When you look in the write-to-the-file
and read-the-file
paragraphs, you can see that the read
and write operations take place in the subprograms readfile and
writefil.
When writefil (beginning on line 113) is called, isamfil.dat is already open, and writefil can access it. Two records are written in writefil.
In readfile, the isamfil.dat file is read (again, without the need for the open), some trivial calculations are performed, and a line of data is displayed.
Copyright © 2002 Micro Focus International Limited. All rights reserved.
This document and the proprietary marks and names
used herein are protected by international law.