Post on 03-Jun-2018
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 1/18
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 2/18
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 3/18
File Types
• Sequential
• Indexed
• Relative
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 4/18
Files
• We use the term file, to describe a collection of one or more
occurrences (instances) of a record type (template).• We use the term r ecord, to describe a collection of fields
which record information about an object.
• We use the term field, to describe an item of information
recorded about an object (e.g. StudentName, DateOfBirth).
File
Record
Fields
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 5/18
Select Clause
• The SELECT clause chooses a file in the COBOL program to
be associated with an external data set.
• File name must be identified by an FD or SD entry in the Data
Division.
• A file-name must conform to the rules for a COBOL user-
defined name, must contain at least one alphabetic character,
and must be unique within this program.
• File-Control.
– Select <file name> assign to <device name>
• The ASSIGN clause associates the program's name for a file
with the external name for the actual data file.
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 6/18
Organization Clause
• The ORGANIZATION clause identifies the logicalstructure of the file.
• The logical structure is established at the time the file
is created and cannot subsequently be changed.
• ORGANIZATION IS SEQUENTIAL (format 1)
– A predecessor-successor relationship among the
records in the file is established by the order inwhich records are placed in the file when it iscreated or extended.
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 7/18
Organization Clause
• ORGANIZATION IS INDEXED (format 2)
– The position of each logical record in the file is determined
by indexes created with the file and maintained by the
system.
– The indexes are based on embedded keys within the file's
records.
• ORGANIZATION IS RELATIVE (format 3)
–
The position of each logical record in the file is determined by its relative record number.
• If you omit the ORGANIZATION clause, the compiler
assumes ORGANIZATION IS SEQUENTIAL.
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 8/18
Access Mode Clause
• Sequential-access mode
– Allows reading and writing records of a file in a serialmanner; the order of reference is implicitly determined bythe position of a record in the file.
• Random-access mode – Allows reading and writing records in a programmer-
specified manner; the control of successive references tothe file is expressed by specifically defined keys supplied
by the user.
• Dynamic-access mode
– Allows the specific input-output statement to determine theaccess mode. Therefore, records can be processedsequentially and/or randomly.
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 9/18
Access Mode Clause
• File organization is the permanent logical structure of the file.
You tell the computer how to retrieve records from the file by
specifying the access mode
• Sequential files
– Files with sequential organization can be accessed only
sequentially. The sequence in which records are accessed is
the order in which the records were originally written.
• Indexed files
– All three access modes are allowed.
• Relative files
– All three access modes are allowed.
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 10/18
Other Clauses
• The RECORD KEY clause specifies the data item within therecord that is the prime RECORD KEY for an indexed file.
• The values contained in the prime RECORD KEY data itemmust be unique among records in the file.
• The ALTERNATE RECORD KEY clause specifies a dataitem within the record that provides an alternative path to thedata in an indexed file.
• The RELATIVE KEY clause identifies a data-name thatspecifies the relative record number for a specific logicalrecord within a relative file.
• The FILE STATUS clause monitors the execution of eachinput-output operation for the file.
• When the FILE STATUS clause is specified, the systemmoves a value into the status key data item after each input-output operation that explicitly or implicitly refers to this file.
• The value indicates the status of execution of the statement.
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 11/18
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 12/18
Other Clauses
• The DATA RECORDS clause names of data records
associated with this file.
• The RECORD CONTAINS clause indicates the size of each
record.
RECORD CONTAINS integer-1 CHARACTERS
The BLOCK CONTAINS clause is included in the File
Description entry only for files on magnetic media
A group of logical records is included within one block tomaximize the efficient use of a disk area.
BLOCK CONTAINS integer-1 RECORDS
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 13/18
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 14/18
Close Verb
• Closes the file opened for processing
• Close <file name>
• You must ensure that, before terminating, your program closes
all the files it has opened. Failure to do so may result in some
data not being written to the file or users being prevented from
accessing the file.
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 15/18
Read Verb
• The READ copies a record occurrence/instance from the file
and places it in the record buffer.
• When the READ statement is executed, the associated file
must be open in INPUT or I-O mode.
• Read <file name> At end <statement>
• When the READ attempts to read a record from the file and
encounters the end of file marker, the AT END is triggered
and the StatementBlock following the AT END is executed
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 16/18
Read Verb
• Execution of a READ statement on sequential files retrievesthe next logical record from the file.
• Execution of a READ statement on indexed files causes thevalue of the key of reference to be compared with the value of
the corresponding key data item in the file records, until thefirst record having an equal value is found.
• The file position indicator is positioned to this record, which isthen made available.
• Execution of a READ statement on relative files sets the file position indicator pointer to the record whose relative recordnumber is contained in the RELATIVE KEY data item, andmakes that record available.
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 17/18
Write Verb
• The WRITE statement releases a logical record for an outputor input/output file.
• When the WRITE statement is executed:
– The associated sequential file must be open in OUTPUT or
EXTEND mode. – The associated indexed or relative file must be open in
OUTPUT, I-O, or EXTEND mode.
• To WRITE data to a file we must move the data to the record
buffer (declared in the FD entry) and then WRITE the contentsof record buffer to the file.
• Write <Record Name>
8/12/2019 Cob Basics 6
http://slidepdf.com/reader/full/cob-basics-6 18/18
File Status Codes
• 00 Successful completion
• 10 AT END Condition
• 30 Permanent error condition
•
34 Boundary Violation