FILE-1

19
Introduct Introduct ion ion to to Files Files

Transcript of FILE-1

Page 1: FILE-1

IntroductionIntroductionto to

Files Files

Page 2: FILE-1

Files, Records, Fields.Files, Records, Fields. We use the term We use the term FIELDFIELD to describe an item of to describe an item of

information we are recording about an objectinformation we are recording about an object (e.g. StudentName, DateOfBirth, CourseCode).

We use the term We use the term RECORDRECORD to describe the collection of to describe the collection of fields which record information about an object 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 We use the term FILE FILE to describe a collection of one to describe a collection of one or more occurrences (instances) of a record type or more occurrences (instances) of a record type (template).(template).

It is important to distinguish between the record It is important to distinguish between the record occurrence (i.e. the values of a record) and the record occurrence (i.e. the values of a record) and the record type (i.e. the structure of the record). type (i.e. the structure of the record). Every record in a file has a Every record in a file has a different valuedifferent value but the but the same structuresame structure..

Page 3: FILE-1

Files, Records, Fields.Files, Records, Fields.

StudId StudName DateOfBirthStudId StudName DateOfBirth9723456 COUGHLAN 100919619724567 RYAN 311219769534118 COFFEY 230619649423458 O'BRIEN 031119799312876 SMITH 12121976

STUDENTS.DATSTUDENTS.DAT

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

occurrencesoccurrences

Record Type Record Type (Template)(Template)(Structure)(Structure)

Page 4: FILE-1

FILE CHARACTERISTICSFILE CHARACTERISTICS The task of file handling is the responsibility of the The task of file handling is the responsibility of the

system software known assystem software known as IOCS (Input-Output IOCS (Input-Output Control System).Control System).

Through the COBOL features, the programmer Through the COBOL features, the programmer should only specify the various should only specify the various file characteristics file characteristics or attributesor attributes to enable the IOCS to handle the file to enable the IOCS to handle the file effectively.effectively.

So before discussing the COBOL features for file So before discussing the COBOL features for file handling, we shall discuss the file characteristics.handling, we shall discuss the file characteristics.

Record SizeRecord Size Block SizeBlock Size BuffersBuffers Label Records/Disk Directory Label Records/Disk Directory

Page 5: FILE-1

A data file is normally created on a Magnetic-tape or A data file is normally created on a Magnetic-tape or Magnetic-disk for the subsequent reading of the file Magnetic-disk for the subsequent reading of the file either in the same program or in some other either in the same program or in some other programs.programs.

A sequential file , is a file whose records can be A sequential file , is a file whose records can be accessed in the order of their appearance in the accessed in the order of their appearance in the files. This logic (sequential manner) is independent files. This logic (sequential manner) is independent of the medium used to store a sequential file.of the medium used to store a sequential file.

A magnetic tape file, such as a card or printer file, A magnetic tape file, such as a card or printer file, can only have a sequential organization.can only have a sequential organization.

A disk file , can have different organizations A disk file , can have different organizations including the sequential one.including the sequential one.

Page 6: FILE-1

Record SizeRecord Size

The records of a card file must consist of The records of a card file must consist of 80 80 characterscharacters and those for a file on the printer should and those for a file on the printer should consist of consist of 132 characters132 characters..

The size of the records in a disk file, on the other The size of the records in a disk file, on the other hand, may be chosen by the programmer while hand, may be chosen by the programmer while preparing the record layout.preparing the record layout.

The programmer should fix suitable sizes for the The programmer should fix suitable sizes for the individual fields in the record.individual fields in the record.

The total of these sizes is the The total of these sizes is the record size.record size. Also we can fix the size of record. Eg, no record Also we can fix the size of record. Eg, no record

should exceed 2048 characters in length or no should exceed 2048 characters in length or no record should be less than 2 characters in length.record should be less than 2 characters in length.

A sequential file can contain either A sequential file can contain either fixed or variable fixed or variable length recordslength records..

Page 7: FILE-1

Most applications use Most applications use fixed-length recordsfixed-length records.. But sometimes it is required that records of different But sometimes it is required that records of different

lengths should be stored onto the file.lengths should be stored onto the file. In such cases, the IOCS normally stores the length In such cases, the IOCS normally stores the length

of each record along with the data in the record.of each record along with the data in the record. Since, the record size is not fixed, the maximum and Since, the record size is not fixed, the maximum and

minimum record sizes are considered as the file minimum record sizes are considered as the file attribute.attribute.

Block SizeBlock Size

While handling a tape or disk file, normally a single While handling a tape or disk file, normally a single record is not read or written.record is not read or written.

Instead, the usual practice is to group a number of Instead, the usual practice is to group a number of consecutive records to form what is known as a consecutive records to form what is known as a block or physical recordblock or physical record..

Page 8: FILE-1

For eg, a block can consist of 10 records. This For eg, a block can consist of 10 records. This means that the first 10 consecutive records will means that the first 10 consecutive records will form the first block of the file, the next 10 form the first block of the file, the next 10 consecutive records will form the second block of consecutive records will form the second block of the file and so on.the file and so on.

The number of records in a block is often called The number of records in a block is often called ““blocking factorblocking factor”.”.

The IOCS takes care of the blocking.The IOCS takes care of the blocking. When a file is blocked, a physical read or write When a file is blocked, a physical read or write

operation on the file is only applicable to the entire operation on the file is only applicable to the entire block and not to the individual records in the block.block and not to the individual records in the block.

But the programmer would like to read or write only But the programmer would like to read or write only one record at a time.one record at a time.

For this the IOCS reserves a memory space equal to For this the IOCS reserves a memory space equal to the size of a block of the file. This memory space is the size of a block of the file. This memory space is known as the known as the bufferbuffer..

Page 9: FILE-1

When the programmer wants to read the first record When the programmer wants to read the first record from the file, the IOCS reads the first block into the from the file, the IOCS reads the first block into the buffer but releases only the first record to the buffer but releases only the first record to the program.program.

Thus the read/write statements in the program are Thus the read/write statements in the program are logical operations, the physical reading or writing is logical operations, the physical reading or writing is done by the IOCS at the block level.done by the IOCS at the block level.

The records as defined in the program are The records as defined in the program are sometimes called logical records and the blocks sometimes called logical records and the blocks which are records as stored on the file medium are which are records as stored on the file medium are called physical records.called physical records.

Two advantage of blocking,Two advantage of blocking, Blocking results in saving in terms of input-output Blocking results in saving in terms of input-output

time required to handle a file.time required to handle a file. Substantial amount of storage space on the tape or Substantial amount of storage space on the tape or

disk can be saved.disk can be saved.

Page 10: FILE-1

In the case of tape files, an In the case of tape files, an inter-record gapinter-record gap is is generated between any two consecutive records. generated between any two consecutive records. The total spaced occupied by these inter-record The total spaced occupied by these inter-record gaps is quite substantial.gaps is quite substantial.

Blocking helps to get the number of such gaps Blocking helps to get the number of such gaps reduced thereby decreasing the wastage of storage reduced thereby decreasing the wastage of storage space on account of these gaps.space on account of these gaps.

BuffersBuffers::

Modern computers are capable of handling I-O Modern computers are capable of handling I-O operations independently by means of the hardware operations independently by means of the hardware known as known as data channeldata channel..

This enables the overlapping of I-O operations with This enables the overlapping of I-O operations with other CPU operations.other CPU operations.

To take advantage of the situation, the IOCS To take advantage of the situation, the IOCS normally requires more than one buffer for a file.normally requires more than one buffer for a file.

Page 11: FILE-1

How files are processed.How files are processed. Files are repositories of data that reside on backing Files are repositories of data that reside on backing

storage (hard disk or magnetic tape).storage (hard disk or magnetic tape). A file may consist of hundreds of thousands or even A file may consist of hundreds of thousands or even

millions of records.millions of records. Suppose we want to keep information about all the TV Suppose we want to keep information about all the TV

license holders in the country. Suppose each record license holders in the country. Suppose each record is about 150 characters/bytes long. If we estimate the is about 150 characters/bytes long. If we estimate the number of licenses at 1 million this gives us a size for number of licenses at 1 million this gives us a size for the file of 150 X 1,000,000 = the file of 150 X 1,000,000 = 150 megabytes150 megabytes..

If we want to process a file of this size we cannot do If we want to process a file of this size we cannot do it by loading the whole file into the computer’s it by loading the whole file into the computer’s memory at once.memory at once.

Files are processed by reading them into the Files are processed by reading them into the computer’s memory computer’s memory one recordone record at a time.at a time.

Page 12: FILE-1

Record BuffersRecord Buffers

To process a file records are read from the file into To process a file records are read from the file into the computer’s memory the computer’s memory one record at a timeone record at a time..

The computer uses the programmers description of The computer uses the programmers description of the record (i.e. the record template) to set aside the record (i.e. the record template) to set aside sufficient memory to store sufficient memory to store one instanceone instance of the of the record.record.

Memory allocated for storing a record is usually Memory allocated for storing a record is usually called a “called a “record bufferrecord buffer””

The record buffer is theThe record buffer is the onlyonly connection between connection between the program and the records in the file.the program and the records in the file.

Page 13: FILE-1

Record BuffersRecord Buffers

IDENTIFICATION DIVISION.etc.ENVIRONMENT DIVISION.etc.DATA DIVISION.FILE SECTION.

ProgramProgram

RecordBufferRecordBuffer DeclarationDeclaration

STUDENTS.DAT

DISK Record Instance

Page 14: FILE-1

Implications of ‘Buffers’Implications of ‘Buffers’ If your program processes more than one file you If your program processes more than one file you

will have to describe a record buffer for will have to describe a record buffer for eacheach file. file. To process all the records in an To process all the records in an INPUT file INPUT file each each

record instance must be copied (read) from the file record instance must be copied (read) from the file into the record buffer when required.into the record buffer when required.

To create an To create an OUTPUT file OUTPUT file containing data records containing data records each record must be placed in the record buffer each record must be placed in the record buffer and then transferred (written) to the file.and then transferred (written) to the file.

To transfer a record from an input file to an output To transfer a record from an input file to an output file we will have tofile 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

Page 15: FILE-1

Creating a Student RecordCreating a Student Record

01 StudentDetails. Student Id.Student Id. 02 StudentId PIC 9(7). Student Name.Student Name. 02 StudentName.

SurnameSurname 03 Surname PIC X(8).InitialsInitials 03 Initials PIC XX.

Date of BirthDate of Birth 02 DateOfBirth.Year of BirthYear of Birth 03 YOBirth PIC 99.Month of BirthMonth of Birth 03 MOBirth PIC 99.Day of BirthDay of Birth 03 DOBirth PIC 99.

Course CodeCourse Code 02 CourseCode PIC X(4). Value of grant Value of grant 02 Grant PIC 9(4). GenderGender 02 Gender PIC X.

Student Details.Student Details.

Page 16: FILE-1

Describing the record buffer in COBOLDescribing the record buffer in COBOL

The record type/template/buffer of The record type/template/buffer of everyevery file used in a file used in a program program mustmust be described in the FILE SECTION by be described in the FILE SECTION by means of an FD (file description) entry.means of an FD (file description) entry.

The FD entry consists of the letters FD and an internal file The FD entry consists of the letters FD and an internal file name.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.

Page 17: FILE-1

STUDENTS.DAT

The Select and Assign Clause.The Select and Assign Clause.

The internal file name used in the FD entry is connected to an 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 external file (on disk or tape) by means of the Select and Assign clause.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

Page 18: FILE-1

Select and Assign Syntax.Select and Assign Syntax.

LINE SEQUENTIALLINE SEQUENTIAL means each record is followed by the means each record is followed by the carriage return and line feed characters.carriage return and line feed characters.

RECORD SEQUENTIALRECORD SEQUENTIAL means that the file consists of a means that the file consists of a stream of bytes. Only the fact that we know the size of each stream of bytes. Only the fact that we know the size of each record allows us to retrieve them. record allows us to retrieve them.

SELECT FileName ASSIGN TO ExternalFileReference

[ORGANIZATION IS LINERECORD

SEQUENTIAL].

Page 19: FILE-1

COBOL file handling VerbsCOBOL file handling Verbs OPENOPEN

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

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

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

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