Sunteți pe pagina 1din 53

FILE STRUCTURE

LAB MANUAL
COMPILED AND EDITED
BY:
MANJULA H N
SINDHU NAIR
YASMIN N S

1) Write a C++ 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. Reapeat 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.
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class names
{
public:
char name[255];
};
void revr(ofstream &out,char name[255])
{
char *rev;
rev=name+strlen(name)-1;
while(rev>=name)
{
out<<*rev;
rev--;
}
}
void revs(char name[255])
{
char *rev;
rev=name+strlen(name)-1;
while(rev>=name)
{
cout<<*rev;
rev--;
}
}
int main()
{
names n[10];
ofstream outf;
ifstream in;
int choice;
while(1)
{
cout<<"enter 1. for standard i/p o/p\n2. to read from
file\n3.exit\n";
cin>>choice;
switch(choice)
{
case 1: {
int m;
cout<<"enter no. of names\n";

cin>>m;
for(int i=0;i<m;i++)
{
cout<<"\nenter the names\n";
cin>>n[i].name;
revs(n[i].name);
cout<<"\n";
}
cout<<"\n";
break;
}
case 2: {
ofstream o;
ifstream in;
char sfile[10], dfile[10];
char ch[10];
cout<<"Enter the input file :";
cin>>sfile;
cout<<"Enter the outut file :";
cin>>dfile;
o.open(sfile,ios::out);
int m;
cout<<"enter no. of names\n";
cin>>m;
for(int i=0;i<m;i++)
{
cout<<"\nenter the names\n";
cin>>n[i].name;
o<<n[i].name<<endl;
}
o.close();
in.open(sfile,ios::in);
ofstream outf(dfile,ios::out);
while(in)
{
in.getline(ch,255);
if(in)
revr(outf,ch);
outf<<"\n";
}
cout<<"\n";
in.close();
}
case 3:
exit(0);
}
}

break;

Output:
[root@localhost ~]# ./a.out
Enter 1. For standard i/p o/p
2. To read from file

3. Exit
1
Enter no. of names
2
enter the names
yasmin
nimsay
enter the names
manjula
alujnam
Enter 1. For standard i/p o/p
2. To read from file
3. Exit
2
Enter the input file: input
Enter the output file: output
Enter no. of names
1
Enter the names
xyz
Enter 1. For standard i/p o/p
2. To read from file
3. Exit
2) Wite a C++ program to read and write student objects with fixedlength records and the fields delmited by |. Implement pack(),
unpack(), modify() and search() methods.
#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
void
void
void

insert();
display();
modify(char *key);
search(char *key);

void Student::getData() {
cout << "Enter usn, name, sem, marks: \n";
cin >> usn >> name >> sem >> marks;
}
void Student::putData() {
cout << usn << "\t" << name << "\t\t" << sem << "\t" << marks <<
endl;
}
void Student::pack() {
strcpy(buffer, usn);
strcat(buffer, "|");
strcat(buffer, name);
strcat(buffer, "|");
strcat(buffer, sem);
strcat(buffer, "|");
strcat(buffer, marks);
while(strlen(buffer) < SIZE - 1) {
strcat(buffer, "#");
}
//here len of buffer is SIZE - 1
strcat(buffer, "\n");
//now len of buffer becomes = SIZE
}
void Student::unpack() {
char *p;
p = strtok(buffer, "|");
p = strtok(NULL, "|");
p = strtok(NULL, "|");
p = strtok(NULL, "#");
}

strcpy(usn, p);
strcpy(name, p);
strcpy(sem, p);
strcpy(marks, p);

void Student::insert() {
getData();
pack();
//packs the data into buffer
ofstream fout("record.txt", ios::app);
fout << buffer;
fout.close();
}
void Student::display() {
ifstream fin("record.txt");
while(!fin.eof()) {
fin.getline(buffer, SIZE + 1, '\n');
if(fin.fail())
break;
unpack();
putData();
}
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: " << count << endl;
fin.close();

void 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;
}
if(count == 0)
cout << "USN not found." << endl;
else
cout << "Modified. " << endl;

fin.close();
fout.close();
remove("record.txt");
rename("temp.txt", "record.txt");

int 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;
case 3:

}
}

cout << "Enter the


cin >> key;
s.search(key);
cout << "Done!" <<
break;
case 4:
cout << "Enter the
cin >> key;
s.modify(key);
cout << "Done!" <<
break;
default:
return 0;

Output:
[root@localhost ~]# ./a.out
1.Insert
2.Display
3.Search
4.Modify
5.Exit
1
Enter usn, name, sem, marks:
1at08is01
ajay
5
25
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit
2
The contents are:
1at08is01
ajay
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit

3
Enter the key USN: 1at08is01
1at08is01
ajay
5
Total records found: 1
Done!
1.Insert
2.Display
3.Search

25

25

key USN: ";


endl;
USN to modify: ";
endl;

4.Modify
5.Exit
4
Enter the USN to modify: 1at08is01
Enter usn, name, sem, marks:
1at08is01
ajit
5
25
Modified.
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit
2
The contents are:
1at08is01
ajit
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit

25

3) Wite a C++ program to read and write student objects with VariableLength records using any suitable record structure. Implement pack(),
unpack(), modify() and search() methods.
#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
void
void
void

insert();
display();
modify(char *key);
search(char *key);

};
void Student::getData() {

cout << "Enter usn, name, sem, marks: \n";


cin >> usn >> name >> sem >> marks;
}
void Student::putData() {
cout << usn << "\t" << name << "\t\t" << sem << "\t" << marks <<
endl;
}
void Student::pack()
strcpy(buffer,
strcat(buffer,
strcat(buffer,
strcat(buffer,
strcat(buffer,
}

{
usn);
name);
sem);
marks);
"\n");

strcat(buffer, "|");
strcat(buffer, "|");
strcat(buffer, "|");

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, "\n"); strcpy(marks, p);
}
void Student::insert() {
getData();
pack();
//packs the data into buffer
ofstream fout("record.txt", ios::app);
fout << buffer;
fout.close();
}
void Student::display() {
ifstream fin("record.txt");
while(!fin.eof()) {
fin.getline(buffer, SIZE + 1, '\n');
if(fin.fail())
break;
unpack();
putData();
}
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: " << count << endl;

fin.close();

void 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;
}
if(count == 0)
cout << "USN not found." << endl;
else
cout << "Modified. " << endl;

fin.close();
fout.close();
remove("record.txt");
rename("temp.txt", "record.txt");

int main() {
int choice;
Student s;
char key[15];
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;
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
1
Enter usn, name, sem, marks:
1at08is059
vimala
5
25
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit
2
The contents are:
1at08is059 vimala
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit

3
Enter the key USN: 1at08is059
1at08is059 vimala
5
Total records found: 1
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit
4
Enter the USN to modify: 1at08is059
Enter usn, name, sem, marks:

25

25

1at08is059
vijay
5
25
Modified.
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit
2
The contents are:
1at08is059 vijay
Done!
1.Insert
2.Display
3.Search
4.Modify
5.Exit

25

4) Write a C++ 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.
#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
void
void
void
void
void

read_data();
pack();
write_to_file();
unpack();
create_rrn();
search_by_rrn(int);

};
void student::read_data()
{
cout<<"\nUsn:";
cin>>USN;
cout<<"\nName:";
cin>>Name;
cout<<"\nBranch:";

cin>>Branch;
cout<<"\nSemster:";
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;
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++];
sem.erase();
i++;
while (buffer[i]!='$')
sem+=buffer[i++];
istringstream out(sem);
out>>Semester;
}
void student::create_rrn()
{
ifstream file;

int pos;
count=-1;
file.open("1.txt",ios::in);
while (!file.eof())
{
pos=file.tellg();
buffer.erase();
getline(file,buffer);
rrn_list[++count]=pos;
}
file.close();

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);
cout<<"\n"<<buffer<<"\n" ;
}
}
int main()
{
int choice,rrn;
student s1;
s1.create_rrn();
while(1){
cout <<"\nMain Menu\n 1.Add \n\n 2.Search 3.EXIT\n\nEnter the
choice:";
cin>>choice;
switch (choice)
{
case 1: cout<<"Data\n";
s1.read_data();
s1.pack();
s1.write_to_file();
break;
case 2: cout <<"\n\nEnter the RRN";
cin>>rrn;
s1.search_by_rrn(rrn);
break;
case 3: return 0;
default:cout<<"\n\nWrong Choice";
break;
}
}
}

Output:
[root@localhost ~]# ./a.out
Main Menu
1.Add
2.Search 3.EXIT
Enter the choice:1
Data
Usn:1at08is058
Name:varsha
Branch:CSE
Semster:5
Main Menu
1.Add
2.Search 3.EXIT
Enter the choice:2
Enter the RRN0
1at08is059|Vimala|ISE|5$
Main Menu
1.Add
2.Search 3.EXIT
Enter the choice:
5) Wite a C++ program to implement simple index on primary key for a
file of students objects. Implement add(), search(), delete() using the
index.
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
class primary_index
{
public:
string usn_list[100];
int address_list[100];
int count;
void create_primary_index();
void insert();
void remove(string);

void search(string);
int search_primary_index(string);
string extract_usn(string);
void 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;
int i=0;
usn.erase();
while(buffer[i]!='|')
usn+=buffer[i++];
return usn;
}
void 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:";
cin>>name;
cout<<"\n BrANCH:";
cin>>branch;
cout<<"\nSEMESTER:";
cin>>semester;
stringstream out;
out<<semester;
sem=out.str();
buffer.erase();
buffer=usn+'|'+name+'|'+branch+'|'+sem+'$'+'\n';
file.open("hi1.txt",ios::out|ios::app);
pos=file.tellp();
file<<buffer;
file.close();
usn_list[++count]=usn;
address_list[count]=pos;
sort_primary_index();
}
int 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;
}
if(flag) return mid;
else return -1;
}
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.";
}
void primary_index::remove(string key)
{
int pos=0;
int address,i;
char del_ch='*';
fstream file;
pos=search_primary_index(key);
if(pos>=0)
{
file.open("hi1.txt");
address=address_list[pos];
file.seekp(address,ios::beg);
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);
break;
case 3:
cout<<"Enter the usn to delete\n";
cin>>key;
i1.remove(key);
break;
case 4:

}
}
return 0;

return 0;
default:
cout<<"Wrong choice\n";

}
Output:
[root@localhost ~]# ./a.out
MAin menu
1.ADD
2.SEARCH
3.Delete
4.Exit
Enter your choice
1
Data
USN:1at08is0458
NAME:ashwini
BrANCH:ise
SEMESTER:5
MAin menu
1.ADD
2.SEARCH
3.Delete
4.Exit
Enter your choice
2
Enter the usn to be searched
1at08is0458
Found the record1at08is0458|ashwini|ise|5$
MAin menu
1.ADD
2.SEARCH
3.Delete
4.Exit
Enter your choice
3
Enter the usn to delete
1at08is0458
Record deleted.
MAin menu
1.ADD
2.SEARCH
3.Delete
4.Exit
Enter your choice:

6) Write a C++ program to implement index on secondary key, the name,


for a file of student objects. Implement add(), search(), delete() using
the secondary index.
#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;
void create_index();
void insert() ;
void remove(string);
void delete_from_file(int);
void search(string);
int search_index(string);
void read_from_file(int);
string extract_Name(string);
void sort_index();
};
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();
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;
}
}
}
}
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;
Address_list[count]=pos;
sort_index();

}
int secondary_index::search_index(string key){
int low=0,high=count,mid=0,flag=0;
while(low<=high){
mid=(low+high)/2;
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
return -1;
}
void 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
cout<<"\n"<<"Not found";
}
void secondary_index::read_from_file(int pos){
int address;
string buffer;
fstream file;
file.open("1.txt");
address=Address_list[pos];
file.seekp(address,ios::beg);
getline(file,buffer);
cout<<endl<<"Found the record:"<<buffer;
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
cout<<"\n\nNot found\n";
}
void secondary_index::delete_from_file(int pos){
char del_ch='*';
int i,address;
if(pos>=0){
fstream file;
file.open("1.txt");
address=Address_list[pos];
file.seekp(address,ios::beg);
file.put(del_ch);
cout<<endl<<"\n\nRecord deleted:";
file.close();
}
for(int i=pos;i<count;i++){
Name_list[i]=Name_list[i+1];
Address_list[i]=Address_list[i+1];
}
count--;
}
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;
i1.remove(key);
break;
case 4: return 0;
default:cout<<"Wrong Choice!!!!!!!\n\n";
}
}

}
Output:
[root@localhost ~]# ./a.out
Main Menu
1:Add
2:Search
3:Delete
4:Exit
Enter the choice1
Data
USN:1at08is059
Name:varsha
Branch:ise
SEMESTER:5
Main Menu
1:Add
2:Search
3:Delete
4:Exit
Enter the choice2
Enter the name
varsha
Found the record:1at08is059|varsha|ise|5$
Main Menu
1:Add
2:Search
3:Delete
4:Exit
Enter the choice3
Enter the Namevarsha
Found the record:1at08is059|varsha|ise|5$
Record deleted:
Main Menu
1:Add
2:Search
3:Delete
4:Exit
Enter the choice:
7) Write a C++ program to read two lists of names and then match the
names in the two lists using Cosequntial Match based on a single loop.
Output the names common to both the lists.
#include<iostream>
#include<fstream>

#include<string.h>
//#include<conio.h>
using namespace std;
int main() {
int i, n;
char name[20], name2[20];
ofstream fout;
ifstream fin1, fin2;
//

clrscr();

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;
while(!fin1.eof() && !fin2.eof()) {
if(strcmp(name, name2) == 0) {
fout << name << endl;
cout <<name <<endl;
fin1 >> name;
fin2 >> name2;
} else if(strcmp(name, name2) < 0) {
fin1 >> name;
} else {
fin2 >> name2;
}
}

fin1.close();
fin2.close();
fout.close();
cout << "Done!";
//

getch();
return 0;

}
OutPut:
[root@localhost ~]# ./a.out
Enter the no of names to enter in record1: 2
Enter 2 names in ascending order:

sindhu
yasmin
Enter the no of names to enter in record2: 2
Enter 2 names in ascending order:
manjula
yasmin
yasmin
8) Write a C++ program to read k lists of names and merge them using kway merge algorithm with k=8.
#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
while(!f1.eof()) //Unpack and retrieve first file
{
f1.getline(recrd[k].name,20,'|');
f1.getline(recrd[k++].usn,20,'\n');
}
while(!f2.eof()) //Unpack and retrieve second file
{
f2.getline(recrd[k].name,20,'|');
f2.getline(recrd [k++].usn, 20,'\n');
}
record temp;
int t,y;
for(t=0;t<k-2;t++) //Sort the retrieved records
for(y=0;y<k-t-2;y++)
if(strcmp(recrd[y].name,recrd[y+1].name)>0)
{
temp=recrd[y];
recrd[y]=recrd[y+1];
recrd[y+1]=temp;
}
fstream temp1;
temp1.open(filename,ios::out);
//Open the file to be packed into
for(t=1;t<k-1;t++) //Pack the sorted records onto the file
temp1<<recrd[t].name<<"|"<<recrd[t].usn<<"\n";

f1.close();
f2.close();
temp1.close();
return;
}
void kwaymerge()
{
char filename[7]
[20]={"ll.txt","22.txt","33.txt","44.txt","lll.txt","222.txt","llll.txt"
};
int i;
int k;
k=0;
for(i=0;i<8;i+=2) //Merge and sort the 8 original files onto
{ //the four files indicated {ll.txt,22.txt.}
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]);
return;
}
int main()
{
int i;
cout<<"Enter the no. of records : ";
cin>>no;
cout<<"\nEnter the details : \n";
for(i=0;i<8;i++) //Create 8 files to store the split data
file[i].open(fname[i],ios::out);
for(i=0;i<no;i++) //Split and pack data onto the files
{
cout<<"Name :"; cin>>rec[i].name;
cout<<"USN : ";cin>>rec[i].usn;
file[i%8]<<rec[i].name<<'|'<<rec[i].usn<<"\n";
}
for(i=0;i<8;i++)
file[i].close();
kwaymerge(); //Merge
fstream result;
result.open("llll.txt",ios::in);
cout<<"\nSorted Records : \n";
char name[20],usn[20];
for(i=0;i<no;i++) //Unpack the sorted records and dispL
{
result.getline(name,20,'|');
result.getline(usn,20,'\n');
cout<<"\nName
: "<<name<<"\nUSN : "<<usn<<"\n";
}
return 0;
}

Output:
[root@localhost ~]# ./a.out
Enter the no. of records : 6
Enter the details :
Name :sindhu
USN : 23
Name :yasmin
USN : 25
Name :manjula
USN : 26
Name :kala
USN : 27
Name :vijetha
USN : 28
Name :priya
USN : 29
Sorted Records :
Name
: kala
USN : 27
Name
: manjula
USN : 26
Name
: priya
USN : 29
Name
: sindhu
USN : 23
Name
: vijetha
USN : 28
Name
: yasmin
USN : 25
9) Write a C++ program to implement B-Tree for a given set of integers
and its operations insert() and search(). Display the tree.
#include<iostream>
#include<math.h>
using namespace std;
struct node
{
int ele[4];
int child[4];
};
class btree
{
public:
node *tree[10][10];
int count[10];
int leaf;

int path[10];
btree()
{
leaf=-1;
for(int i=0;i<10;i++)
{
count[i]=-1;
path[i]=-1;
}
}
void search(int key)
{
int i,j,k,temp;
path[0]=0;
if(leaf)
{
j=0;
for(i=0;i<leaf;i++)
{
temp=search_node(tree[i][j],key);
path[i+1]=temp;
j=temp;
}
}
}
int search_node(node *node1,int key)
{

for(int i=0;i<4;i++)
{
if(key<=node1->ele[i])
return node1->child[i];
else if(node1->ele[i+1]==-1)
return node1->child[i];
}
}
int nodefull(node *node1)
{
if(node1->ele[3]!=-1)
return 1;
else
return 0;
}
void insert_node(node *node1,int key)
{
int flag=0,count=-1,i,j,x,y,l,t;
node *newnode,*n1;
for(i=0;i<4;i++)
if(node1->ele[i]!=-1)
++count;
i=0;
while(!flag&& node1->ele[i]!=-1)
{
if(node1->ele[i]>key)
{
flag=1;
for(int j=count;j>=i;j--)
node1->ele[j+1]=node1->ele[j];

node1->ele[i]=key;
}
i++;

}
if(!flag)
{
node1->ele[count+1]=key;
for(i=leaf-1;i>=0;i--)
{
n1=tree[i][path[i]];
for(t=0;n1->ele[t]!=-1;t++);
n1->ele[t-1]=key;
}
}
for(i=0;i<=count+1;i++)
cout<<"\t\t"<<node1->ele[i];
}
void split(node *oldnode)
{
node *newnode,*parent,*n1,*n2;
double t1;
int i,j,k,n,t,x,y,pos;
newnode=create_node();
newnode->ele[0]=oldnode->ele[2];
newnode->ele[1]=oldnode->ele[3];
oldnode->ele[2]=-1;
oldnode->ele[3]=-1;
t=count[leaf];
n=path[leaf];
for(i=t,j=t+1;i>n;i--,j--)
tree[leaf][j]=tree[leaf][i];
tree[leaf][n+1]=newnode;
count[leaf]++;
x=leaf;
if(count[leaf]+1==1)
t=1;
else
{
t1=(double)t;
t1=log((double)count[leaf]+1)/log(2.0);
t=(int)t1;
}
if(t!=leaf)
{
++leaf;
count[leaf]=count[x];
for(i=0;i<=count[leaf];i++)
std::swap(tree[leaf][i],tree[x][i]);

}
for(i=leaf-1;i>=0;i--)
count[i]=-1;
for(i=t,j=i-1;i>0;i--,j--)
{

for(k=0;k<(count[i]

+1)/2;k++)

{
+1];

n1=tree[i][2*k];
n2=tree[i][(2*k)
for(x=0;n1-

>ele[x]!=-1;x++);

for(y=0;n2-

>ele[y]!=-1;y++);
newnode=create_node();

count[j]++;
tree[j]
[count[j]]=newnode;

newnode-

>ele[0]=n1->ele[x-1];

newnode-

>child[0]=2*k;

newnode-

>ele[1]=n2->ele[y-1];

newnode-

>child[1]=(2*k)+1;

}
if(count[i]!=1&&count[i]

%2==0)

{
[count[i]];

n2=tree[i]
for(y=0;n2-

>ele[y]!=-1;y++);

newnode-

>ele[2]=n2->ele[y-1];

newnode-

>child[2]=count[i];

}
}

}
node *create_node()
{
node *n;
n=new node;
for(int i=0;i<4;i++)
{
n->ele[i]=-1;
n->child[i]=-1;
}
return n;
}
void insert(int key)
{
int n,parent;
node *first_node;
if(leaf==-1)
{
first_node=create_node();
tree[0][0]=first_node;

leaf++;
count[0]++;
first_node->ele[0]=key;

}
else if(leaf==0)
{
if(nodefull(tree[0][0]))
{
path[leaf]=0;
split(tree[0][0]);
insert(key);
}
else
insert_node(tree[0][0],key);
}
else
{
search(key);
n=path[leaf];
parent=path[leaf-1];
if((nodefull(tree[leaf][n])))
{
split(tree[leaf][n]);
insert(key);
}
else
insert_node(tree[leaf][n],key);
}
}

void main_search(int key)


{
int flag=0,i;
node *node1;
search(key);
node1=tree[leaf][path[leaf]];
for(i=0;node1->ele[i]!=-1;i++)
if(node1->ele[i]==key)
{
flag=1;
break;
}
cout<<"\nThe path traversed is:";
for(i=0;path[i]!=-1;i++)
cout<<path[i]<<"->";
if(flag)
cout<<"\nElement found";
else
cout<<"\nNot found";
}
void display_tree()
{
int i,j,k;
for(i=0;i<=leaf;i++)
{
cout<<"\n\nLevel--------"<<i<<"\n";
for(j=0;j<=count[i];j++)
{

}
}

for(k=0;tree[i][j]->ele[k]!=-1;k++)
cout<<" "<<tree[i][j]->ele[k];
cout<<"\t";

}
};
int main()
{
btree bt;
int choice,key;
while(1)
{
cout<<"\n\nMain
Menu\n------------------------\n1.Insert\n2.Search\n3.Display
tree\n4.Exit\nEnter ur choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\nEnter the element:";
cin>>key;
bt.insert(key);
break;
case 2:cout<<"Enter the key:";
cin>>key;
bt.main_search(key);
break;
case 3:bt.display_tree();
break;
case 4:return 0;
default:cout<<"\nEnter valid choice";
}
}
}
Output:
[root@localhost ~]# ./a.out
Main Menu
-----------------------1.Insert
2.Search
3.Display tree
4.Exit
Enter ur choice:1
Enter the element:10
Main Menu
-----------------------1.Insert
2.Search
3.Display tree
4.Exit

Enter ur choice:1
Enter the element:34
10

34

Main Menu
-----------------------1.Insert
2.Search
3.Display tree
4.Exit
Enter ur choice:1
Enter the element:45
10

34

45

Main Menu
-----------------------1.Insert
2.Search
3.Display tree
4.Exit
Enter ur choice:1
Enter the element:78
10

34

45

Main Menu
-----------------------1.Insert
2.Search
3.Display tree
4.Exit
Enter ur choice:1
Enter the element:45
45
Main Menu
-----------------------1.Insert
2.Search
3.Display tree
4.Exit
Enter ur choice:3

45

Level--------0
34 78
Level--------1
10 34
45 45 78
Main Menu
-----------------------1.Insert
2.Search

78

78

3.Display tree
4.Exit
Enter ur choice:2
Enter the key:45
The path traversed is:0->1->
Element found
Main Menu
-----------------------1.Insert
2.Search
3.Display tree
4.Exit
Enter ur choice:
10) Write a C++ program to implement B+Tree for a given set of integers
and its operations insert() and search(). Display the tree.
#include<iostream>
#include<cmath>
using namespace std;
struct node
{
int ele[4];
int child[4];
node *next;
};
class bptree
{
public:node *tree[10][10];
int count[10];
int leaf;
int path[10];
node *head;
bptree()
{
leaf=-1;
for(int i=0;i<10;i++)
{count[i]=-1;path[i]=-1;}
}
node* create_node()
{
node *n;
n=new node;
for(int i=0;i<4;i++)
{
n->ele[i]=-1;
n->child[i]=-1;
}
n->next=NULL;
return n;
}
void insert(int key)
{
int n,parent;

node *first_node;
if(leaf==-1)
{
first_node=create_node();
tree[0][0]=first_node;
leaf++;
count[0]++;
first_node->ele[0]=key;
head=first_node;
}
else if(leaf==0)
{
if(nodefull(tree[0][0]))
{
path[leaf]=0;
split(tree[0][0]);
insert(key);
}
else insert_node(tree[0][0],key);
}
else
{
search(key);
n=path[leaf];
parent=path[leaf-1];
if((nodefull(tree[leaf][n])))
{
split(tree[leaf][n]);
insert(key);
}
else
insert_node(tree[leaf][n],key);
}

}
void main_search(int key)
{
int flag=0,i;
node *node1;
search(key);
node1=tree[leaf][path[leaf]];
for(i=0;node1->ele[i]!=-1;i++)
if(node1->ele[i]==key)
{
flag=1;
break;
}
cout<<"\nthe path traversed is: ";
for(i=0;path[i]!=-1;i++)
cout<<path[i]<<" -> ";
if(flag) cout<<"\nelement found";
else
cout<<"\n not found";
}
void display_tree()
{
int i,j,k;
for(i=0;i<=leaf;i++)

cout<<"\n\nlevel---------" <<i<<"\n";
for(j=0;j<=count[i];j++)
{
if(i!=leaf)
k=1;
else
k=0;
for(;tree[i][j]->ele[k]!=-1;k++)
cout<<" "<<tree[i][j]->ele[k];
cout<<"\t";
}

}
}
void search(int key)
{
int i,j,temp;
path[0]=0;
if(leaf)
{
j=0;
for(i=0;i<leaf;i++)
{
temp=search_node(tree[i][j],key);
path[i+1]=temp;
j=temp;
}
}
}
int search_node(node *node1,int key)
{
if(key<=node1->ele[0])
return node1->child[0];
for(int i=1;i<4;i++)
{
if((key >= node1->ele[i]) && (key<node1>ele[i+1]))

return node1->child[i];
else if(node1->ele[i+1]==-1)
return node1->child[i];
}
}
int nodefull(node *node1)
{
if(node1->ele[3]!=-1)
return 1;
else
return 0;
}
void insert_node(node *node1,int key)
{
int flag=0,count=-1,i,j,x,y,l;
node *newnode,*parent;
for(i=0;i<4;i++)
if(node1->ele[i]!=-1)
++count;
i=0;

while(!flag && node1->ele[i]!=-1)


{
if(node1->ele[i]>key)
{
flag=1;
for(int j=count;j>=i;j--)
node1->ele[j+1]=node1->ele[j];
node1->ele[i]=key;
}
i++;
}
if(!flag)
node1->ele[count+1]=key;
if(node1->ele[0]==key)
{
for(i=leaf-1;i>=0;i--)
{
x=path[i+1];
if(tree[i][path[i]]->ele[x]>key) tree[i]
[path[i]]->ele[x]=key;
else insert_node(tree[i][x],key);
}
}
for(i=0;i<=count+1;i++)
cout<<"\t\t"<<node1->ele[i];
}
void split(node *oldnode)
{
node *newnode,*parent,*n1,*n2;
int i,j,t1,k,n,t,x,y,pos;
newnode=create_node();
newnode->ele[0]=oldnode->ele[2];
newnode->ele[1]=oldnode->ele[3];
oldnode->ele[2]=-1;
oldnode->ele[3]=-1;
t=count[leaf];
n=path[leaf];
for(i=t,j=t+1;i>n;i--,j--)
tree[leaf][j]=tree[leaf][i];
newnode->next=tree[leaf][n]->next;
tree[leaf][n]->next=newnode;
tree[leaf][n+1]=newnode;
count[leaf]++;
x=leaf;
if(count[leaf]+1==1) t=1;
else
{
t1=(double)t;
t1=log(count[leaf]+1)/log(2);
t=(int)t1;
}
if(t!=leaf)
{
++leaf;
count[leaf]=count[x];

for(i=0;i<=count[leaf];i++)
std::swap(tree[leaf][i],tree[x][i]);
}
for(i=leaf-1;i>=0;i--) count[i]=-1;
for(i=t,j=i-1;i>0;i--,j--)
{
for(k=0;k<=count[i]/3;k++)
{
n1=tree[i][2*k];
n2=tree[i][(2*k)+1];
newnode=create_node();
count[j]++;
tree[j][count[j]]=newnode;
newnode->ele[0]=n1->ele[0];
newnode->child[0]=2*k;
newnode->ele[1]=n2->ele[0];
newnode->child[1]=(2*k)+1;
}
if(count[i]!=1 && count[i]%2==0)
{
n2=tree[i][count[i]];
newnode->ele[2]=n2->ele[0];
newnode->child[2]=count[i];
}

}
}
void display_seqset()
{
node *t;
int k;
t=head;
cout<<"\n\nsequence set is:";
while(t)
{
for(k=0;t->ele[k]!=-1;k++)
cout<<" "<<t->ele[k];
cout<<"\t";
t=t->next;
}
}

};
int main()
{
bptree bt;
int choice,key;
while(1)
{
cout<<"\n\n\n main menu\n----------------------\n1insert\n2-search\n3-display tree\n4-display sequence set\n5exit\n\nenter choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\nenter element:";
cin>>key;
bt.insert(key);

}
}

break;
case 2:cout<<"enter the key:";
cin>>key;
bt.main_search(key);
break;
case 3:bt.display_tree();
break;
case 4:bt.display_seqset();
break;
case 5:return 0;
default:cout<<"\n enter valid choice";

Output:
[root@localhost ~]# ./a.out

main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:1
enter element:12

main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:1
enter element:34
12
main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit

34

enter choice:1
enter element:56
12

34

56

34

56

78

89

56

78

main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:1
enter element:78
12

78

main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:1
enter element:89
56
main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:1
enter element:23
23
main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:1

89

enter element:45
23

45

56

main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:3
level---------0
23 78
level---------1
12 34
23 45 56

78 89

main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:2
enter the key:78
the path traversed is: 0 -> 2 ->
element found
main menu
---------------------1-insert
2-search
3-display tree
4-display sequence set
5-exit
enter choice:
11) Write a C++ program to store and retrieve student data from file
using hashing. Use any collision resolution technique.
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;

#define maxrecord 100


char hash_table[maxrecord][30],buffer[30];
char tombstone[]={"####"};
class student
{
public: char key[5],name[20],sem[2];
void read()
{
cout<<"enter record key\n";
cin>>key;
cout<<"enter the name\n";
cin>>name;
cout<<"enter the semester\n";
cin>>sem;
return;
}
void pack()
{
strcpy(buffer,key);
strcat(buffer,"|");
strcat(buffer,name);
strcat(buffer,"|");
strcat(buffer,sem);
strcat(buffer,"|");
return;
}
void retrieve(int addr,char k[])
{
int found=0,i;
i=addr;
do
{
if(strcmp(hash_table[i],tombstone)==0)
break;
unpack(hash_table[i]);
if(strcmp(key,k)==0)
{
found=1;
break;
}
else
{
i++;
if(i==maxrecord)
i=0;
}
}
while(i!=addr);
if(found==0)
cout<<"record does not exists in hash file\n";
else
cout<<"record found
\n\n"<<key<<"\t"<<name<<"\t"<<sem<<"\n\n";
return;
}

void unpack(char buffer[])


{
int k=0,i;
for(i=0;buffer[i]!='|';i++)
key[k++]=buffer[i];
key[k]='\0';
k=0;
for(++i;buffer[i]!='|';i++)
name[k++]=buffer[i];
name[k]='\0';
k=0;
for(++i;buffer[i]!='|';i++)
sem[k++]=buffer[i];
sem[k]='\0';
return;
}
void initialize()
{
for(int i=0;i<maxrecord;i++)
strcpy(hash_table[i],tombstone);
return;
}
void store(int addr)
{
int flag=0,i;
if(strcmp(hash_table[addr],tombstone)==0)
{
strcpy(hash_table[addr],buffer);
flag=1;
cout<<"record is insert at its home address:
"<<addr<<"\n";
}
else
for(i=addr+1;i!=addr;i++)
{
if(i%maxrecord==0)
i=0;
if(strcmp(hash_table[i],tombstone==0))
{
cout<<"collision has occured\n";
cout<<"home is "<<addr<<" actual
address is "<<i<<"\n";
strcpy(hash_table[i],buffer);
flag=1;
break;
}
}
if(i==addr&&(!flag))
cout<<"hash file is full,record cannot be
insert\n";
return;
}
}s;

int hash(char key[])


{
int i=0,sum=0;
while(key[i]!='\0')
{
sum+=key[i]-48;
i++;
}
return sum%99;
}
int main()
{
int choice,addr;
char kk[5];
s.initialize();
while(1)
{
cout<<"\n 1. store\n2. retrieve\n3. exit\nenter your choice:
";
cin>>choice;
switch(choice)
{
case 1: s.read();
s.pack();
addr=hash(s.key);
s.store(addr);
break;
case 2: cout<<"enter the key value\n";
cin>>kk;
addr=hash(kk);
s.retrieve(addr,kk);
break;
default:exit(0);
}
}
return 0;
}
Output:
[root@localhost ~]# ./a.out
1. store
2. retrieve
3. exit
enter your choice: 1
enter record key
12
enter the name
tina
enter the semester
5
record is insert at its home address: 3
1. store
2. retrieve

3. exit
enter your choice: 1
enter record key
34
enter the name
manju
enter the semester
5
record is insert at its home address: 7
1. store
2. retrieve
3. exit
enter your choice: 2
enter the key value
34
record found
34

manju 5

1. store
2. retrieve
3. exit
enter your choice:
12) Write a C++ program to reclaim the free space resulting from the
deletion of records using linked lists.
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;
char filename[]={"d1.txt"};
class student
{
char usn[11];
char name[20];
char sem[2];
char buffer[50];
public:
friend istream & operator>>(istream &,student &);
friend ostream &operator<<(ostream &,student &);
void pack();
void unpack();
void read();
void write();
void delet(char*);
int search(char u[]);
void useoffset(int x);
}s;
istream & operator>>(istream &in,student &stud)
{
cout<<"enter the usn of the student"<<endl;
cin>>stud.usn;
cout<<"enter the name of the student"<<endl;

cin>>stud.name;
cout<<"enter the sem of the student"<<endl;
cin>>stud.sem;
return in;
}
ostream &operator<<(ostream &out,student &stud)
{
out<<"usn:"<<stud.usn<<"\tname:"<<stud.name<<"\tsem:"<<stud.sem<<endl;
return out;
}
void student::pack()
{
int i;
strcpy(buffer,usn);
strcat(buffer,"|");
strcat(buffer,name);
strcat(buffer,"|");
strcat(buffer,sem);
strcat(buffer,"|");
for(i=strlen(buffer);i<50;i++)
{
buffer[i]='_';
}
}
int student::search(char u[])
{
int flag=0,pos;
fstream fin;
fin.open(filename,ios::in);
if(!fin)
{
cout<<"unable to open the file"<<endl;
return -1;
}
while(1)
{
pos=fin.tellg();
fin.getline(buffer,51);
if(fin.eof())
break;
if(buffer[0]=='$')
continue;
unpack();
if(strcmp(usn,u)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<*this;
fin.close();
return pos;
}

else
return -1;
}
void student::unpack()
{
int i,j=0;
for(i=0;buffer[j]!='|';i++,j++)
{
usn[i]=buffer[j];
}
usn[i]='\0';
j++;
for(i=0;buffer[j]!='|';i++,j++)
{
name[i]=buffer[j];
}
name[i]='\0';
j++;
for(i=0;buffer[j]!='|';i++,j++)
{
sem[i]=buffer[j];
}
sem[i]='\0';
j++;
}
void student::read()
{
fstream fin;
fin.open(filename,ios::in);
if(!fin)
{
cout<<"unable to open the file"<<endl;
return;
}
while(1)
{
fin.getline(buffer,51);
if(fin.eof())
break;
unpack();
cout<<*this;
}
fin.close();
}
void student::write()
{
fstream fout;
fout.open(filename,ios::out|ios::app);
if(!fout)
{
cout<<"unable to open the file"<<endl;
return;
}
cin>>*this;
pack();
fout<<buffer<<endl;
fout.close();

}
void student::delet(char u[])
{
int pos;
pos=search(u);
if(pos==-1)
{
cout<<"no such record exists";
return;
}
else
{
fstream fout;
fout.open(filename,ios::out|ios::in);
if(!fout)
{
cout<<"unable to open the file";
return;
}
fout.seekg(pos,ios::beg);
fout.getline(buffer,51);
buffer[0]='$';
fout.seekp(pos,ios::beg);
fout<<buffer;
fout.close();
}
}
void student::useoffset(int x)
{
fstream fout;
fout.open(filename,ios::out|ios::in);
fout.seekg(x,ios::beg);
fout.getline(buffer,51);
cin>>*this;
pack();
fout.seekp(x,ios::beg);
fout<<buffer;
fout.close();
}
class node
{
public:
int offset;
node *link;
};
class list
{
node *head;
node *curr;
node *newnode;
public:
list();
void delet(char u[]);
void use();
}l;
list::list()
{

head='\0';
}
void list::delet(char u[])
{
int pos=s.search(u);
if(pos==-1)
{
cout<<"no such record found\n";
return;
}
newnode=new node;
s.delet(u);
newnode->link='\0';
newnode->offset=pos;
if(head==NULL)
{
head=newnode;
}
else
{
newnode->link=head;
head=newnode;
}
}
void list::use()
{
if(head=='\0')
{
s.write();
}
else
{
curr=head;
s.useoffset(curr->offset);
head=head->link;
delete curr;
}
}
int main()
{
int choice;
char u[11];
while(1)
{
cout<<"enter the choice 1.insert(pack) 2.reclaim space
3.display(unpack)4.delete 5.exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:s.write();
break;
case 2:l.use();
break;
case 3:s.read();
break;
case 4:cout<<"enter the usn\n";
cin>>u;

l.delet(u);
break;
case 5:exit(0);
break;
}
}
return 0;
}

Output:
[root@localhost ~]# ./a.out
enter the choice 1.insert(pack) 2.reclaim
3.display(unpack)4.delete 5.exit
1
enter the usn of the student
1
enter the name of the student
aditya
enter the sem of the student
5
enter the choice 1.insert(pack) 2.reclaim
3.display(unpack)4.delete 5.exit
1
enter the usn of the student
2
enter the name of the student
wilma
enter the sem of the student
5
enter the choice 1.insert(pack) 2.reclaim
3.display(unpack)4.delete 5.exit
3
usn:1 name:aditya sem:5
usn:2 name:wilma sem:5
enter the choice 1.insert(pack) 2.reclaim
3.display(unpack)4.delete 5.exit
4
enter the usn
2
usn:2 name:wilma sem:5
usn:2 name:wilma sem:5
enter the choice 1.insert(pack) 2.reclaim
3.display(unpack)4.delete 5.exit
2
enter the usn of the student
4
enter the name of the student
xyz
enter the sem of the student
5
enter the choice 1.insert(pack) 2.reclaim
3.display(unpack)4.delete 5.exit
3
usn:1 name:aditya sem:5
usn:4 name:xyz
sem:5

space

space

space

space

space

space

enter the choice 1.insert(pack) 2.reclaim space


3.display(unpack)4.delete 5.exit

S-ar putea să vă placă și