Beruflich Dokumente
Kultur Dokumente
6th Semester
ACADEMIC YEAR
2017 - 2018
LABORATORY MANUAL
USN
Section / Batch
www.atria.edu
6th Semester
ACADEMIC YEAR
2018
2017 –
Section / Batch
Prepared by
PROF. OM PRAKASH B
LABORATORY CERTIFICATE
Date:
File Structures Laboratory 15ISL68
PREFACE
The idea of this manual is to make students aware of programs in the File
sequential processing and Hashing. This manual is therefore an attempt to fill the
gap
in the knowledge of the students. The most important feature of this lab manual
is
given the coding part of each technique of File structures along with the suitable
sample inputs and outputs. It is also suggests various Object oriented concepts
using
C++. Written in with proper indentation, students find this manual extremely
useful
and handy. An incline of the questions for their viva is given at the end of this
book.
Though all the efforts have been made to make this manual error free, yet
some
errors might have crept in inadvertently. Suggestions from the teachers and
students
Hoping that this manual will help the students develop a new perspective
about
sorting technique to arrange the data and Select suitable indexing and hashing
experience.
Prof. OMPRAKASH B
SYLLABUS
PART A
1. Write a program to read series of names, one per line, from standard input and
write these names spelled in reverse order to the standard output using I/O
redirection and pipes. Repeat the exercise using an input file specified by the
user instead of the standard input and using an output file specified by the user
instead of the standard output.
2. Write a program to read and write student objects with fixed-length records
and the fields delimited by “|”. Implement pack ( ), unpack ( ), modify ( ) and
search ( ) methods
3. Write a program to read and write student objects with Variable - Length
records using any suitable record structure. Implement pack ( ), unpack ( ),
modify ( ) and search ( ) methods.
4. Write a program to write student objects with Variable - Length records using
any suitable record structure and to read from this file a student record using
RRN.
5. Write a program to implement simple index on primary key for a file of student
objects. Implement add ( ), search ( ), delete ( ) using the index.
6. Write a program to implement index on secondary key, the name, for a file of
student objects. Implement add ( ), search ( ), delete ( ) using the secondary
index.
7. Write a program to read two lists of names and then match the names in the
two lists using Consequential Match based on a single loop. Output the names
common to both the lists.
8. Write a program to read k Lists of names and merge them using k-way merge
algorithm with k = 8.
Part B
Mini Project -- Student should develop mini project on the topics mentioned
below or similar applications Document processing, transaction management,
indexing and hashing, buffer management, configuration management. Not
limited to these.
CONTENTS
1 I/O Redirection 1
2 Fixed-Length Records 7
3 Variable-Length Records 15
8 K-Way Merge 49
1) Write a program to read a series of names, one per line, from standard
input and write these names spelled in reverse order to the standard output
using I/O redirection and pipes. Repeat the exercise using an input file
specified by the user instead of the standard input and using an output file
specified by the user instead of the standard output.
Program:
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace
std; class names {
public:
revs(char name[255]) {
char *rev;
rev=name+strlen(name)-1;
while(rev>=name) {
cout<<*rev; rev--; } }
int main() {
break; } case 2: {
ofstream o; ifstream in; char
sfile[10], dfile[10]; char
ch[10]; cout<<"Enter the
input file :";
4 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
Output: [root@localhost
~]# ./a.out Enter 1. For
standard i/p o/p 2. To read
Pack(): This method is used to group all the related field values of particular
record taken by the application in buffer.
Unpack(): This method is used to ungroup all the related field values of
particular record taken from the file in buffer.
Program:
#include<iostream>
#include<fstream>
#include<string.h>
#define SIZE 55 using
namespace std; char
buffer[SIZE + 1];
class Student {
char usn[15]; char name[20];
char sem[5]; char marks[10];
public: void getData(); void
putData(); void pack(); void
unpack(); void insert(); void
display(); void modify(char
*key); void search(char *key);
}; void Student::getData() {
Student::putData() {
cout << usn << "\t" << name << "\t\t" << sem << "\t" << marks <<
void Student::unpack() {
char *p; p = strtok(buffer, "|"); strcpy(usn, p); p =
strtok(NULL, "|"); strcpy(name, p); p = strtok(NULL,
"|"); strcpy(sem, p); p = strtok(NULL, "#");
ifstream fin("record.txt");
while(!fin.eof()) {
fin.getline(buffer, SIZE + 1, '\n');
if(fin.fail()) break; unpack(); putData();
}
9 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
fin.close(); } void
Student::search(char *key) {
ifstream fin("record.txt");
int count = 0;
while(!fin.eof()) {
fin.getline(buffer, SIZE + 1, '\n');
if(fin.fail()) break; unpack();
if(strcmp(usn, key) == 0) {
putData(); count++; } } cout << "Total records found: "
Student::modify(char *key) {
ifstream fin("record.txt");
ofstream fout("temp.txt");
int count = 0;
while(!fin.eof()) {
fin.getline(buffer, SIZE + 1, '\n');
if(fin.fail()) break; unpack();
if(strcmp(usn, key) == 0) {
getData(); count++; }
pack(); fout << buffer; }
10 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
if(count == 0)
cout << "USN not found." << endl; else
cout << "Modified. " << endl;
fin.close(); fout.close();
remove("record.txt");
main() {
int choice; Student
s; char key[15]; //
clrscr();
while(1) {
cout << "1.Insert\n"
<< "2.Display\n" <<
"3.Search\n" << "4.Modify\n"
<< "5.Exit\n" << endl; cin >>
choice; switch(choice) {
case 1:
s.insert(); cout << "Done!" <<
endl; break; case 2:
cout << "The contents are: " <<
endl; s.display(); cout << "Done!" <<
endl; break;
11 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
case 3:
cout << "Enter the key USN: "; cin
>> key; s.search(key); cout <<
"Done!" << endl; break; case 4:
cout << "Enter the USN to modify: "; cin
>> key; s.modify(key); cout << "Done!" <<
endl; break; default:
return 0; } } } Output:
[root@localhost ~]# ./a.out
1.Insert 2.Display 3.Search
4.Modify 5.Exit
marks: 1at08is01
ajay 5 25
Done! 1.Insert
2.Display
3.Search
4.Modify
5.Exit
2.Display
3.Search
4.Modify
5.Exit
class Student {
char usn[15];
char name[20];
char sem[5];
char marks[10];
public: void
getData();
15 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
void putData();
void pack(); void
unpack();
Student::getData() {
cout << "Enter usn, name, sem, marks: \n"; cin
Student::putData() {
cout << usn << "\t" << name << "\t\t" << sem << "\t" << marks <<
Student::unpack() {
char *p; p = strtok(buffer, "|"); strcpy(usn, p); p
= strtok(NULL, "|"); strcpy(name, p); p =
strtok(NULL, "|"); strcpy(sem, p); p =
strtok(NULL, "\n"); strcpy(marks, p); }
16 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
ifstream fin("record.txt");
while(!fin.eof()) {
fin.getline(buffer, SIZE + 1, '\n'); if(fin.fail())
break; unpack(); putData(); } fin.close(); }
fin.close(); fout.close();
remove("record.txt");
main() {
int choice;
Student s;
char key[15];
while(1) {
cout << "1.Insert\n"
<< "2.Display\n"
<< "3.Search\n"
18 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
Output: [root@localhost
~]# ./a.out 1.Insert
2.Display 3.Search
4.Modify 5.Exit
marks: 1at08is059
vimala 5
25 Done! 1.Insert
2.Display
3.Search 4.Modify 5.Exit
2 The contents are: 1at08is059
vimala
5 25 Done! 1.Insert 2.Display 3.Search
1at08is059 1at08is059
vimala 5 25
Total records found: 1
20 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
Program: #include
<iostream> #include
<fstream> #include
<string> #include
<sstream> using
namespace std; class
student {
public:
string USN;
string Name;
string Branch;
int Semester;
string buffer; int
count;
int rrn_list[100];
void read_data();
void pack();
22 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
void student::read_data() {
cout<<"\nUsn:";
cin>>USN;
cout<<"\nName:";
cin>>Name;
cout<<"\nBranch:";
cin>>Branch;
cout<<"\semester:";
cin>>Semester; } void
student::pack() {
string sem; stringstream out; out << Semester; sem =
out.str(); buffer.erase();
buffer=USN+'|'+Name+'|'+Branch+'|'+sem+'$'+'\n'; }
void student::write_to_file() {
int pos; fstream file;
file.open("1.txt",ios::out|ios::app);
pos=file.tellp(); file<<buffer;
23 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
file.close();
rrn_list[++count]=pos; } void
student::unpack() {
string sem; int
ch=1,i=0;
USN.erase(); while
(buffer[i]!='|')
USN+=buffer[i++];
Name.erase(); i++;
while (buffer[i]!='|')
Name+=buffer[i++];
Branch.erase(); i++;
while (buffer[i]!='|')
Branch+=buffer[i++];
} void
student::create_rrn() {
ifstream file;
24 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
} void student::search_by_rrn(int
rrn) {
int pos=-1; fstream file; if (rrn>count) cout<<"\n
Not Found"; else{ buffer.erase();
file.open("1.txt"); pos=rrn_list[rrn];
file.seekp(pos,ios::beg); getline(file,buffer);
int choice,rrn;
student s1;
s1.create_rrn();
25 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
break; } } }
Output: [root@localhost
~]# ./a.out Main Menu
1.Add
2.Search
3.EXIT
Main Menu
1.Add
2.Search
3.EXIT
Index A structure containing a set of entries, each consisting of a key field and a
reference field, Which is used to locate records in a data file. Key field The part
of an index which contains keys. Reference field The part of an index which
contains information to locate records.
• An index imposes order on a file without rearranging the
file.
• Indexing works by indirection. Simple
Index for Entry-Sequenced Files
Simple index
• An index in which the entries are a key ordered linear list. Simple indexing can
be useful when the entire index can be held in memory. Changes (additions and
deletions) require both the index and the data file to be changed.
• Updates affect the index if the key field is changed, or if the record is moved.
An update which moves a record can be handled as a deletion followed by an
addition.
Primary key A primary key is a special relational database table column (or
combination of columns) designated to uniquely identify all table records. A
primary key's main features are: It must contain a unique value for each row of
data. It cannot contain null values. Program: #include<iostream>
#include<fstream> #include<string> #include<sstream> using namespace std;
class primary_index {
public:
sort_primary_index(); }; void
primary_index::create_primary_index() {
fstream file; int pos; string
buffer,usn; count=-1;
file.open("hi1.txt",ios::in);
while(!file.eof()) {
pos=file.tellg(); buffer.erase();
getline(file,buffer); if(buffer.empty()) break;
usn=extract_usn(buffer);
usn_list[++count]=usn;
address_list[count]=pos; } file.close();
sort_primary_index(); } string
primary_index::extract_usn(string buffer) {
string usn;
29 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
primary_index::sort_primary_index() {
int i,j,temp_address;
string temp_usn;
for(i=0;i<=count;i++) {
for(j=i+1;j<=count;j++) {
if(usn_list[i]>usn_list[j]) {
temp_usn=usn_list[i]; usn_list[i]=usn_list[j];
usn_list[j]=temp_usn; temp_address=address_list[i];
address_list[i]=address_list[j];
address_list[j]=temp_address; } } } } void
primary_index::insert() {
string
usn,name,branch,sem,buffer; int
semester,address,pos; fstream
file; cout<<"\nUSN:"; cin>>usn;
cout<<"\nNAME:";
30 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
primary_index::search_primary_index(string key) {
int low=0,high=count,mid=0,flag=0,pos;
while(low<=high) {
mid=(low+high)/2;
if(usn_list[mid]==key) {
flag=1; break; }
if(usn_list[mid]>key)
high=mid-1;
if(usn_list[mid]<key)
low=mid+1; }
31 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
void primary_index::search(string
key) {
int pos=0,address; string buffer;
fstream file; buffer.erase();
pos=search_primary_index(key)
; if(pos>=0) {
file.open("hi1.txt");
address=address_list[pos];
file.seekp(address,ios::beg);
getline(file,buffer); cout<<"\nFound the
record"<<buffer; file.close(); } else
cout<<"\nNot found."; }
file.put(del_ch);
cout<<"\nRecord deleted.";
file.close();
for(i=pos;i<count;i++) {
usn_list[i]=usn_list[i+1];
address_list[i]=address_list[i+1]; } count--; }
else
cout<<"Not found.\n"; }
int main() {
int choice; string key;
primary_index i1;
i1.create_primary_index();
while(1) {
cout<<"\nMAin menu\n1.ADD\n2.SEARCH\n";
cout<<"3.Delete\n4.Exit\nEnter your choice\n";
cin>>choice; switch(choice) {
case 1:
cout<<"Data\n";
i1.insert(); break; case
2:
cout<<"Enter the usn to be
searched\n"; cin>>key; i1.search(key);
33 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
break; case 3:
cout<<"Enter the usn to delete\n";
cin>>key; i1.remove(key); break; case
4:
return 0; default:
cout<<"Wrong choice\n"; } } return 0;
} Output:
USN:1at08is045
NAME:ashwini
Branch: ise
SEMESTER:6
to delete 1at08is045
Record deleted.
Main menu 1.ADD
2.SEARCH 3.Delete
4.Exit Enter your
choice: 4
35 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
Index A structure containing a set of entries, each consisting of a key field and a
reference field, Which is used to locate records in a data file. Key field The part
of an index which contains keys. Reference field The part of an index which
contains information to locate records.
• An index imposes order on a file without rearranging the
file.
• Indexing works by indirection. Simple
Index for Entry-Sequenced Files
Simple index
• An index in which the entries are a key ordered linear list. Simple indexing can
be useful when the entire index can be held in memory. Changes (additions and
deletions) require both the index and the data file to be changed.
• Updates affect the index if the key field is changed, or if the record is moved.
An update which moves a record can be handled as a deletion followed by an
addition.
Secondary key A secondary key is made on a field that you would like to be
indexed for faster searches. A table can have more than one secondary key
Program: #include<iostream> #include<fstream> #include<sstream>
#include<string> using namespace std; class secondary_index{
public:
string Name_list[100];
int Address_list[100];
int count;
36 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
}; void secondary_index::create_index()
{
fstream file; int pos;
string buffer,name;
count=-1;
file.open("1.txt",ios::in);
while(!file.eof()) {
pos=file.tellg(); buffer.erase();
getline(file,buffer); if(buffer.empty() ) break;
if(buffer[0]=='*') continue; //imp since it leads
the last \n and goes to new line
name=extract_Name(buffer);
Name_list[++count]=name;
Address_list[count]=pos; }
file.close();
sort_index();
37 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
buffer.erase(); } string
secondary_index::extract_Name(string buffer) {
string USN,Name; int i=0;
USN.erase(); while(buffer[i]!='|')
USN+=buffer[i++]; Name.erase();
i++; while(buffer[i]!='|')
Name+=buffer[i++]; return Name; }
void
secondary_index::sort_index() {
int i,j,temp_Address;
string temp_Name;
for(int i=0;i<count;i++) {
for(int j=i+1;j<=count;j++) {
if(Name_list[i]>Name_list[j]) {
temp_Name=Name_list[i];
Name_list[i]=Name_list[j];
Name_list[j]=temp_Name;
temp_Address=Address_list[i];
Address_list[i]=Address_list[j];
Address_list[j]=temp_Address; }
//End of if
38 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
void secondary_index::insert()
{
string USN,Name,Branch,sem,buffer; int
semester,pos; fstream file ; cout<< "\n USN:";
cin>>USN; cout<< "\n Name:"; cin>>Name; cout<<
"\nBranch:" ; cin>>Branch; cout<< "\nSEMESTER:";
cin>>semester; stringstream out; out<<semester;
sem=out.str();
buffer=USN+'|'+Name+'|'+Branch+'|'+sem+'$'+'\n';
file.open("1.txt",ios::out|ios::app); pos=file.tellp();
file<<buffer; file.close(); Name_list[++count]=Name;
if(Name_list[mid]==key){
flag=1; break; }
if(Name_list[mid]>key)
high=mid-1; else
low=mid+1; }
if(flag){
return mid; } else
secondary_index::search(string key){
int pos=0,t; string buffer;
buffer.erase();
pos=search_index(key);
if(pos>=0){
read_from_file(pos); t=pos;
while(Name_list[++t]==key)
read_from_file(t); t=pos;
while(Name_list[--t]==key)
read_from_file(t); } else
secondary_index::read_from_file(int pos){
int address;
40 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
file.close(); } void
secondary_index::remove(string key){
int pos=0,t; string buffer;
buffer.erase();
pos=search_index(key);
if(pos>=0){
read_from_file(pos);
delete_from_file(pos); t=pos;
while(Name_list[++t]==key){
read_from_file(t);
delete_from_file(t); }
t=pos; while(Name_list[--t]==key){
read_from_file(t);
delete_from_file(t); } } else
secondary_index::delete_from_file(int pos){
char del_ch='*';
int i,address;
41 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
int main(){
int ch; string key;
secondary_index i1;
i1.create_index();
while(1){
cout<<"\nMain Menu\n1:Add\n2:Search\n3:Delete\n4:Exit\nEnter
the choice";
cin>>ch;
switch(ch){
case 1:cout<<"Data \n";
i1.insert(); break; case
2:cout<<"Enter the name\n";
cin>>key; i1.search(key);
break; case 3:cout<<"Enter the
Name";
cin>>key;
42 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
Found the
record:1at08is059|varsha|ise|5$ Main
Menu 1:Add 2:Search
43 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
Found the
record:1at08is059|varsha|ise|5$ Record
deleted: Main Menu 1:Add 2:Search
3:Delete 4:Exit Enter the choice: 4
Program:
#include<iostream>
#include<fstream>
#include<string.h>
//#include<conio.h>
using namespace
std;
int main() {
fout.open("record1.txt");
cout << "Enter the no of names to enter in record1: ";
cin >> n; cout << "Enter " << n << " names in
ascending order: \n"; for(i=0; i<n; i++) { cin >> name;
fout << name << endl; } fout.close();
fout.open("record2.txt"); cout << "Enter the no of
names to enter in record2: "; cin >> n; cout << "Enter "
<< n << " names in ascending order: \n"; for(i=0; i<n;
i++) { cin >> name; fout << name << endl; }
fout.close();
fin1.open("record1.txt");
fin2.open("record2.txt");
fout.open("output.txt");
fin1 >> name; fin2 >>
name2;
if(strcmp(name, name2) == 0) {
fout << name << endl;
cout <<name <<endl;
fin1 >> name; fin2 >>
name2;
} else {
fin2 >> name2; } }
fin1.close(); fin2.close();
fout.close(); cout <<
"Done!"; // getch();
return 0;
8) Write a program to read k lists of names and merge them using k-way
merge algorithm with k=8.
Merge The process of forming a list containing all items in any of two or
more lists. K-way merge A merge of order k Order of a merge The
number of input lists being merged.
• If the distribution phase creates k runs, a single k-way merge can be
used to produce the final sorted file.
• A significant amount of seeking is used by a k-way merge, assuming
the input runs are on the same disk. Program: #include<iostream>
#include<fstream> #include<string.h> using namespace std; // Record
specification class record {
public: char name[20]; char usn[20]; }rec[20]; int no; fstream file[8]; //The
first 8 files char fname[8][8] =
{"l.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"}; void
merge_file(char* file1, char* file2, char* filename) {
record recrd[20]; int k; k=0; fstream f1,f2;
f1.open(file1,ios::in); //open the first file
f2.open(file2,ios::in); //open the second file
file {
f2.getline(recrd[k].name,20,'|');
filename[7][20]={"ll.txt","22.txt","33.txt","44.txt","lll.txt","222.txt","llll.txt"}; int
i; int
k;
50 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
merge_file(fname[i],fname[i+1],filename[k++]); } k=4;
for(i=0;i<4;i+=2) //Merge and sort the four files onto lll.txt and
222.txt {
merge_file(filename[i],filename[i+1],filename[k++]);
}
//Merge and sort the two files onto the llll.txt file
merge_file(filename[4],filename[5],filename[6]);
for(i=0;i<8;i++) file[i].close();
kwaymerge(); //Merge
fstream result; result.open("llll.txt",ios::in);
cout<<"\nSorted Records : \n";
51 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
Output: [root@localhost
~]# ./a.out Enter the no. of records : 6
Name : priya
USN : 29
Name : sindhu
USN : 23
Name : vijetha
USN : 28
Name : yasmin
USN : 25
53 Department of Information Science & Engineering, Atria Institute Of
Technology
File Structures Laboratory 15ISL68
VIVA QUESTIONS
69. What are B-trees? 70. Explain Splitting. 71. Explain the logic of
redistribution. 72. Explain how you are deleting the nodes in B trees.
73. Explain the logic for merging the nodes in B trees. 74. Define B*
trees. 75. Explain Virtual B-trees. 76. What is LRU replacement? 77.
What do you mean by replacement based on page height? Explain.
78. What is order of a B-tree? 79. Define page index. 80. What are
blocks? 81. What are separators? 82. Define prefix. 83. Define B+
tree. 84. Define hashing. 85. Define hashing function. 86. Explain the
advantages and disadvantages of hashing. 87. Explain any simple
hashing algorithm. 88. Define Packing Density. 89. What is
Progressive overflow? 90. Define search length. 91. What are
buckets? 92. What are tombstones? 93. Define double hashing. 94.
Define chained progressive overflow. 95. What do you mean by
chaining with a separate overflow Area? 96. Define scatter tables. 97.
What is a pattern of record access? 98. What do you mean by home
address? 99. What is Mid-square method? 100. Define Extendible
hashing? 101. Define buddy buckets? 102. Define linear hashing.
103. Explain dynamic hashing.
56 Department of Information Science & Engineering, Atria Institute Of
Technology