Anda di halaman 1dari 19

Introduction

to
Files
Files, Records, Fields.
We use the term FIELD to describe an item of
information we are recording about an object
(e.g. StudentName, DateOfBirth, CourseCode).
We use the term RECORD to describe the collection
of fields which record information about an object
(e.g. a StudentRecord is a collection of fields recording
information about a student).
We use the term FILE to describe a collection of one
or more occurrences (instances) of a record type
(template).
It is important to distinguish between the record
occurrence (i.e. the values of a record) and the record
type (i.e. the structure of the record).
Every record in a file has a different value but the
same structure.
Files, Records, Fields.
StudId StudName DateOfBirth
9723456 COUGHLAN 10091961
9724567 RYAN 31121976
9534118 COFFEY 23061964
9423458 O'BRIEN 03111979
9312876 SMITH 12121976
STUDENTS.DAT

DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudId PIC 9(7).
02 StudName PIC X(8).
02 DateOfBirth PIC X(8).

occurrences
Record Type
(Template)
(Structure)
FILE CHARACTERISTICS
The task of file handling is the responsibility of the
system software known as IOCS (Input-Output
Control System).
Through the COBOL features, the programmer
should only specify the various file characteristics
or attributes to enable the IOCS to handle the file
effectively.
So before discussing the COBOL features for file
handling, we shall discuss the file characteristics.

Record Size
Block Size
Buffers
Label Records/Disk Directory
A data file is normally created on a Magnetic-tape or
Magnetic-disk for the subsequent reading of the file
either in the same program or in some other
programs.
A sequential file , is a file whose records can be
accessed in the order of their appearance in the
files. This logic (sequential manner) is independent
of the medium used to store a sequential file.
A magnetic tape file, such as a card or printer file,
can only have a sequential organization.
A disk file , can have different organizations
including the sequential one.
Record Size

The records of a card file must consist of 80
characters and those for a file on the printer should
consist of 132 characters.
The size of the records in a disk file, on the other
hand, may be chosen by the programmer while
preparing the record layout.
The programmer should fix suitable sizes for the
individual fields in the record.
The total of these sizes is the record size.
Also we can fix the size of record. Eg, no record
should exceed 2048 characters in length or no
record should be less than 2 characters in length.
A sequential file can contain either fixed or variable
length records.
Most applications use fixed-length records.
But sometimes it is required that records of
different lengths should be stored onto the file.
In such cases, the IOCS normally stores the length
of each record along with the data in the record.
Since, the record size is not fixed, the maximum and
minimum record sizes are considered as the file
attribute.

Block Size

While handling a tape or disk file, normally a single
record is not read or written.
Instead, the usual practice is to group a number of
consecutive records to form what is known as a
block or physical record.
For eg, a block can consist of 10 records. This
means that the first 10 consecutive records will
form the first block of the file, the next 10
consecutive records will form the second block of
the file and so on.
The number of records in a block is often called
blocking factor.
The IOCS takes care of the blocking.
When a file is blocked, a physical read or write
operation on the file is only applicable to the entire
block and not to the individual records in the block.
But the programmer would like to read or write only
one record at a time.
For this the IOCS reserves a memory space equal to
the size of a block of the file. This memory space is
known as the buffer.
When the programmer wants to read the first record
from the file, the IOCS reads the first block into the
buffer but releases only the first record to the
program.
Thus the read/write statements in the program are
logical operations, the physical reading or writing is
done by the IOCS at the block level.
The records as defined in the program are
sometimes called logical records and the blocks
which are records as stored on the file medium are
called physical records.
Two advantage of blocking,
Blocking results in saving in terms of input-output
time required to handle a file.
Substantial amount of storage space on the tape or
disk can be saved.

In the case of tape files, an inter-record gap is
generated between any two consecutive records.
The total spaced occupied by these inter-record
gaps is quite substantial.
Blocking helps to get the number of such gaps
reduced thereby decreasing the wastage of storage
space on account of these gaps.

Buffers:

Modern computers are capable of handling I-O
operations independently by means of the hardware
known as data channel.
This enables the overlapping of I-O operations with
other CPU operations.
To take advantage of the situation, the IOCS
normally requires more than one buffer for a file.
How files are processed.
Files are repositories of data that reside on backing
storage (hard disk or magnetic tape).
A file may consist of hundreds of thousands or even
millions of records.
Suppose we want to keep information about all the TV
license holders in the country. Suppose each record
is about 150 characters/bytes long. If we estimate the
number of licenses at 1 million this gives us a size for
the file of 150 X 1,000,000 = 150 megabytes.
If we want to process a file of this size we cannot do it
by loading the whole file into the computers memory
at once.
Files are processed by reading them into the
computers memory one record at a time.
Record Buffers
To process a file records are read from the file into
the computers memory one record at a time.
The computer uses the programmers description of
the record (i.e. the record template) to set aside
sufficient memory to store one instance of the
record.
Memory allocated for storing a record is usually
called a record buffer
The record buffer is the only connection between
the program and the records in the file.
Record Buffers
IDENTIFICATION DIVISION.
etc.
ENVIRONMENT DIVISION.
etc.
DATA DIVISION.
FILE SECTION.


Program
RecordBuffer
Declaration
STUDENTS.DAT
DISK
Record Instance
Implications of Buffers
If your program processes more than one file you
will have to describe a record buffer for each file.
To process all the records in an INPUT file each
record instance must be copied (read) from the file
into the record buffer when required.
To create an OUTPUT file containing data records
each record must be placed in the record buffer
and then transferred (written) to the file.
To transfer a record from an input file to an output
file we will have to
read the record into the input record buffer
transfer it to the output record buffer
write the data to the output file from the output record
buffer
Creating a Student Record
01 StudentDetails.
Student Id. 02 StudentId
PIC 9(7).
Student Name. 02 StudentName.
Surname 03 Surname
PIC X(8).
Initials 03 Initials
PIC XX.
Date of Birth 02 DateOfBirth.
Year of Birth 03
YOBirth PIC 99.
Month of Birth 03
MOBirth PIC 99.
Day of Birth 03
DOBirth PIC 99.
Course Code 02 CourseCode
PIC X(4).
Value of grant 02 Grant
PIC 9(4).
Gender 02 Gender
PIC X.
Student Details.
Describing the record buffer in COBOL
The record type/template/buffer of every file used in
a program must be described in the FILE SECTION
by means of an FD (file description) entry.
The FD entry consists of the letters FD and an
internal file name.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(2).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
02 Gender PIC X.
STUDENTS.DAT
The Select and Assign Clause.
The internal file name used in the FD entry is
connected to an external file (on disk or tape) by
means of the Select and Assign clause.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile
ASSIGN TO STUDENTS.DAT.

DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(2).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
02 Gender PIC X.
DISK
Select and Assign Syntax.
LINE SEQUENTIAL means each record is followed
by the carriage return and line feed characters.


RECORD SEQUENTIAL means that the file consists
of a stream of bytes. Only the fact that we know
the size of each record allows us to retrieve them.
SELECT FileName ASSIGN TO ExternalFileReference
[ORGANIZATION IS
LINE
RECORD
SEQUENTIAL].

COBOL file handling Verbs


OPEN
Before your program can access the data in an input file or
place data in an output file you must make the file available
to the program by OPENing it.

READ
The READ copies a record occurrence/instance from the
file and places it in the record buffer.

WRITE
The WRITE copies the record it finds in the record buffer
to the file.

CLOSE
You must ensure that (before terminating) your program
closes all the files it has opened. Failure to do so may result
in data not being written to the file or users being
prevented from accessing the file.

Anda mungkin juga menyukai