You are on page 1of 30

Sanjivani Rural Education Society’s

Sanjivani College of Engineering, Kopargaon-423 603


(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NACC ‘A’ Grade Accredited, ISO 9001:2015 Certified

Department of Computer Engineering


(NBA Accredited)

Subject- Data Structures-II(CO214)


Unit VI- File Organization

Prof. S.S.Deore
Assistant Professor
E-mail : deoresandhyacomp@sanjivani.org.in
Contact No: 7744925789
• Sequential file organization- concept and primitive operations, Direct Access File-
Concepts and Primitive operations, Indexed sequential file organization-concept,
types of indices, structure of index sequential file, Linked Organization- multi list
files, coral rings, inverted files and cellular partitions.

• External Sort- Consequential processing and merging two lists, multiway merging-
a k way merge algorithm.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 2


File
All programs we looked earlier:
• Input data from the keyboard.
• Output data to the screen.
• Output would be lost as soon as the program execution ends.

How do we store data permanently?


• We can use secondary storage device.
• Data is packaged up on the storage device as data structures called files.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 3


File is a Collection of record where each record consist of one or
more fields.
e.g. employee File:
Emp_id (number)
Name
Occupation
Qualification
Location
Salary

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 4


• Fixed Length record File : All records are of fixed/same length
Fast access: start address and end address is known.

Too short/too large record

• Variable Length record File: records are of variable length


Delimiter for end of record
Searching is slower

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 5


File Stream in C++

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 6


Stream Classes in C++

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 7


• ifstream – provides input operations on file
-open() default input mode.
-get(),getline(),read(),seekg() & tellg()functions.
• ofstream – provides output operations onfiles
• -open() default output mode.
•-put(),seekp(),tellp() & write
• functions.
• fstream – supports for simultaneous input and output operations on
files

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 8


Stream Classes Functions in C++

• File open
1. Constructors take file name and file-open mode

ofstream outClientFile( "filename", fileOpenMode );

2.Using open method


ofstream ClientFile;
ClientFile.open( "filename", fileOpenMode);

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 9


Two File Streams working on two files

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 10


Two File Streams working on one files

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 11


File mode

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 12


Example
All these flags can be combined using the bitwise
operator OR (|).
fstream file;
file.open ("example.bin", ios::out | ios::app | ios::binary);

Closing a File
file.close();

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 13


End of File

Ifstream object returns 0 when it encounters end of file


while(fin)
Another approach to detect end of File:
if(fin.eof( ) !=0) { exit(1);}
eof(): ios class
eof():returns nonzero value when encounters eof

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 14


Input and Output Operation
put() and get() function the function
file.get(ch);
file.put(ch);
write() and read() function
write() and read() functions write and read
binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 15


File Pointers and Their Manipulation
• ifstream, has a pointer known as the get
• pointer that points to the element to be read in the next input
operation.
• ofstream has a pointer known as the put pointer that points to the
location where the next element has to be written.
• fstream, inherits both, the get and the put pointers, from iostream

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 16


seekg() moves get pointer(input) to a specified location
seekp()
moves put pointer (output) to a specified locati

tellg() gives the current position of the get pointer


tellp() gives the current position of the put pointer

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 17


seekg(offset, refposition );
seekp(offset, refposition );
The refposition takes one of the following three constants
defined in the ios class.

ios::beg start of the file


ios::cur current position of the pointer
ios::end end of the file
example: file.seekg(-10, ios::cur);

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 18


FILE ORGANIZATION
Types of File Organization
• There are three types of organizing the file:
1. Sequential access file organization
2. Direct access file organization
3. Indexed sequential access file organization

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 20


Sequential File Orgnization

• The Sequential file organization is a popular file organization in the database


management system (DBMS).

• It is a simple technique for file organization structure. This technique stores the
data element in the sequence manner that is organized one after another in
binary format.

• The File organization in DBMS supports various data operations such as insert,
update, delete, and retrieve the data.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 21


• The file store the unique data attributes for identification and that helps to place
the data element in the sequence. It is logical sequencing in computer memory
that stores the data element for the database management systems.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 22


• Traditional file method

• It is a standard method for sequential file organization in which the data elements
are inserted one after another in the order those are inserted. And in case of a
new record being inserted, it is placed at the end position of the file that is after
the last inserted data element or record.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 23


• In the scenario of data modification or data deletion operation, the particular
data element is searched through the sequence in the memory blocks, and after
it is found, the update or deletion operation applied to that data element. Also,
for the delete operation, the identified data element is marked for deletion and
the new block of the record is inserted.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 24


• Fig shows file method working process in the sequential file
organization in the database management system.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 25


Sorted Method
• The Sorted file method is another popular method for sequential file organization
in the database management system.

• The data elements are stored as ascending or descending order based upon the
primary key or any other key reference.

• the new data element or the new record is inserted at the end position of the file.
After the inserting step, It then gets shorted in the ascending or the descending
order based upon the key.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 26


DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 27
• For the update or data modification scenario, the data element is searched, and
updated based upon the condition. And, after the update operation completes
the sorting process happens to rearrange the data elements, and the updated
data element is placed at the right position of the sequential file structure.

• Similarly, for deletion operation.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 28


Advantages of sequential
• Reading of records in order of ordering key is extremely efficient.

• Finding the next record in order of the ordering key usually, does not required
additional block access. Next record may be found in the same block.

• Searching operation on ordering key is must faster. Binary search can be utilized.
Binary search will required log2b block accesses where b is the total number of
blocks in the file.
Disadvantages of sequential file
• Sequential file does not give any advantage when the search operation is to be
carried out on non-ordering field.

• Inserting a record is an expensive operation. Insertion of new record requires finding


of place of insertion and then all records ahead of it must be moved to create space
for the record to be inserted. This could be very expensive for large files.

• Deleting a record is an expensive operation. Deletion to required movement of


record.

• Modification of field value of ordering key could be time consuming.

You might also like