Posts Tagged ‘fs lab’

File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science) – Alternative 2

// June 13th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 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

This includes tow files

1.

//scindex.h
class SCIndex
{
public:

int  Insert (const char  *skey,const char  *pkey);
int  Remove (const char * key);
int Search (const char * key) const;
void Print (ostream &);
void Write (ostream &);
void Read(istream &);
void Init (int maxKeys);
private:
int MaxKeys;
int NumKeys;
char  **sKeys;
char  **pKeys;
int Find (const char * key) const;

};

int SCIndex :: Insert (const char *skey,const char *pkey)
{
int i=0,j=0;
if (NumKeys == MaxKeys) return 0; //no room for another key

for (i = NumKeys-1; i >=0; i–)
{

if(strcmp(skey,sKeys[i]) > 0)
break; // insert into location i+1
else if(strcmp(skey,sKeys[i]) == 0)
break; // insert into location i+1

else
{
sKeys[i+1] = sKeys[i];
pKeys[i+1] = pKeys[i];
}
}
j=i;

for(j=i; j>=0; j–)

//   while(strcmp(skey,sKeys[j]) == 0)
{
if(strcmp(pkey,pKeys[j]) > 0)
break;
sKeys[j+1] = sKeys[j];
pKeys[j+1] = pKeys[j];

}
sKeys[j+1] = strdup(skey);
pKeys[j+1] = strdup(pkey);
NumKeys ++;
return 1;
}

void SCIndex::Write(ostream &stream)
{    int i=0;
stream<<NumKeys<<”|”;
for (i = 0; i <NumKeys; i++)
{

stream<<sKeys[i];
stream<<”|”;
stream<<pKeys[i];
stream<<”|”;

}
}

int SCIndex :: Remove (const char * key)
{
int index = Find (key);
if (index < 0) return 0; // key not in index
for (int i = index; i < NumKeys; i++)
{
sKeys[i] = sKeys[i+1];
pKeys[i] = pKeys[i+1];
}
NumKeys –;
return 1;
}

int  SCIndex :: Search (const char * key) const
{
int i=0,fnd=0;
for(i=0;i<NumKeys;i++)
{
if (strcmp(sKeys[i], key)==0)
{
fnd=1;
cout<<endl<<sKeys[i]<<”             “<<pKeys[i];
}
}
if (fnd==0)
return -1;
return 1;

}

void SCIndex :: Print (ostream & stream)
{
stream << “Secondary Index :  Max keys = “<<MaxKeys
<<”    Number of  keys = “<<NumKeys<<endl;
for (int i = 0; i<NumKeys; i++)
stream <<”tKey["<<i<<"] “<<sKeys[i]
<<”  : Primary key =”<<pKeys[i]<<endl;
}

int SCIndex :: Find (const char * key) const
{
for (int i = 0; i < NumKeys; i++)
if (strcmp(sKeys[i], key)==0)
return i;// key found
else if (strcmp(sKeys[i], key)>0) return -1;// not found
return -1;// not found
}

void SCIndex :: Init (int maxKeys)
{

if (maxKeys <= 0)
{
MaxKeys = 0;
cout<<endl<<”No Records : “<<endl;
}
MaxKeys = maxKeys;
sKeys =new char *[maxKeys];
pKeys = new char *[maxKeys];
NumKeys=0;

}

void SCIndex::Read(istream &stream)
{
int i=0;
/*
stream.read(NumKeys,4);
for (i = 0; i <NumKeys; i++)
{

stream.read(sKeys[i],10);
stream.read(pKeys[i],10);
}
*/
}

—————————————————————————————————————–

2. //lab6.cpp

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include “student.h”
#include “varbuf.h”
#include “index.h”
#include “scindex.h”

main()
{

//char buffer[500];

student s;
VariableLengthBuffer b;
TextIndex ind;
SCIndex scind;
fstream datafile,indexp,indexs;
int choice=0,k=0,k1=0,n=0;
int recaddr,i;
char delkey[20],skey[20],srchp[10];
datafile.open(“student.txt”,ios::out|ios::in);
indexp.open(“pstudent.ind”, ios::out);
indexs.open(“sstudent.ind”, ios::out);

b.Init(65);
ind.Init(50);
scind.Init(50);
b.Clear();
cout<<endl<<”Enter number of students : “;
cin>>n;

for(i=1;i<=n;i++)
{
cout<<endl<<” Record : “<<i;
s.clear();
s.readdata();
b.Clear();
b.Pack(s.usn);
b.Pack(s.name);
b.Pack(s.address);
b.Pack(s.sem);
recaddr=datafile.tellg();
k=ind.Insert(s.usn,recaddr);
if( k != 1)
cout<<endl<<” Duplicate Primary key : Record Not Inserted “;
else
{
b.Write(datafile);
k=scind.Insert(s.name,s.usn);
}
}

do
{
cout<<endl;
cout<<endl<<”1.Insert Record “;
cout<<endl<<”2.Delete Record “;
cout<<endl<<”3.Search Record “;
cout<<endl<<”4.Display Primary Index”;
cout<<endl<<”5.Display Secondary Index”;
cout<<endl<<”6.Exit “;
cout<<endl;

cout<<” Enter your choice “;
cin>>choice;
switch(choice)
{
case 1:
cout<<endl;
s.readdata();
datafile.seekg(0,ios::end);
recaddr=datafile.tellg();
k=ind.Insert(s.usn,recaddr);
if(k!=1)
cout<<endl<<”Record not inserted : Duplicate Primary Key “;

else
{
k=scind.Insert(s.name,s.usn);
b.Clear();
b.Pack(s.usn);
b.Pack(s.name);
b.Pack(s.address);
b.Pack(s.sem);
b.Write(datafile);
cout<<endl<<”Record Inserted “;
}
break;

case 2:
cout<<endl;
cout<<” Enter Name of the Record to be deleted : ” ;
cin>>delkey;
cout<<endl<<”Following Record/s Found for : “<<delkey<<endl;
cout<<endl<<”Secondary Key   Primary Key”;
cout<<endl<<”————–  ————-”;
k1=scind.Search(delkey);
if(k1 !=1)
{
cout<<endl<<” Record not found “;
break;
}
else
{
cout<<endl<<” Enter USN : “;
cin>>srchp;

k=scind.Remove(delkey);
k1=ind.Remove(srchp);
if(k!=1 || k1!=1)
cout<<endl<<”Record not Found “;
else
cout<<endl<<”Record of “<<delkey<<” USN “<<srchp<<” Deleted”;

}
break;

case 3:
cout<<endl;
cout<<” Enter name of the student to be Searched : ” ;
cin>>skey;
cout<<endl<<”Following Record/s Found for : “<<skey<<endl;
cout<<endl<<”Secondary Key    Primary Key”;
cout<<endl<<”————–   ————”;

k1=scind.Search(skey);
if(k1 !=1)
{
cout<<endl<<” Record not found “;
break;
}
else
{
cout<<endl<<” Enter USN : “;
cin>>srchp;
recaddr=ind.Search(srchp);
if(recaddr<0)
cout<<endl<<”Record does not exist”;
else
{
cout<<endl<<” Record found “;
cout<<endl<<”Record reference of USN “<<srchp<<” is “<<recaddr<<endl;
datafile.seekg(recaddr,ios::beg);
b.Clear();
b.Read(datafile);
b.bufdisplay();
s.clear();
b.Unpack(s.usn);
b.Unpack(s.name);
b.Unpack(s.address);
b.Unpack(s.sem);
s.display();
s.clear();
datafile.seekg(0,ios::end);
}
}
break;

case 4:
ind.Print(cout);
break;

case 5:
scind.Print(cout);
break;
//case 5: exit(1);
}
}   while(choice < 6);

ind.Write(indexp);
scind.Write(indexs);

indexp.close();
indexs.close();
datafile.close();

}

File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science)- Alternative 1

// June 13th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 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;
//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;
/*for(int i=pos;i<count;i++){
Name_list[i]=Name_list[i+1];
Address_list[i]=Address_list[i+1];
}    */
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,ch;
string buffer;
buffer.erase();
pos=search_index(key);
if(pos>=0){
read_from_file(pos);
cout<<endl<<”delete ?”;
cin>>ch;
if(ch)
delete_from_file(pos);
t=pos;
while(Name_list[++t]==key){
read_from_file(t);
cout<<endl<<”Delete?”;
cin>>ch;
if(ch)
delete_from_file(t);
}
t=pos;
while(Name_list[--t]==key){
read_from_file(t);
cout<<endl<<”Delete?”;
cin>>ch;
if(ch)
delete_from_file(t);
}
}
else
cout<<”nnNot foundn”;
}
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<<”nnRecord 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 Menun1: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 namen”;
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!!!!!!!nn”;

}
}
}

File Structure ( FS ) Lab Program 12 ( 6th Semester Information Science)

// May 31st, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 12 : Write a C++ program to reclaim the free space resulting from the deletion of records using linked lists.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#include<fstream.h>
#include<new.h>

class node
{
public: char name[20];
char usn[20];
node *link;
};

node *first=NULL;

void writeFile()
{
node *p;
char buffer[100];
fstream out;
out.open(“student.txt”, ios::out);
if(!out)
{
cout<<”n Unable to open the file student.txt in out mode”;
getch();
exit(0);
}
p=first;
while(p!=NULL)
{
strcpy(buffer,p->name);
strcat(buffer,”|”);
strcat(buffer,p->usn);
strcat(buffer,”n”);
out<<buffer;
p=p->link;
}
}

void display()
{
node *p;
if(first==NULL)
{
cout<<”nList is empty”;
return;
}
p=first;
while(p!=NULL)
{
cout<<”|”<<p->name<<” “<<p->usn<<”|”<<”->”;
p=p->link;
}
}

void Insert()    //Insert the record at the rear end
{
char name[20],usn[15];
node *p,*q;
cout<<”n Enter name = “; cin>>name;
cout<<”nEnter usn   = “; cin>>usn;
p=new node;
strcpy(p->name,name);
strcpy(p->usn,usn);
p->link=NULL;
if(first==NULL)
{
first=p;
writeFile();
display();    //display the record on the screen
return;
}
for(q=first; q->link!=NULL; q=q->link)
{
;
}
q->link=p;
writeFile(); //writing the record to the file
display();   //display the records to the screen.
}

void Delete()
{
char usn[15];
node *curr,*prev,*del;
if(first==NULL)
{
printf(“nThe list is empty. Deletion is not possible”);
return;
}
cout<<”nEnter the usn to be deleted = “; cin>>usn;
if(strcmp(first->usn,usn)==0)
{
cout<<”n Record deleted”;
del = first;
delete del;
first=first->link;
writeFile();
return;
}
prev=NULL;
curr=first;
while( ( strcmp(curr->usn,usn) != 0 ) && curr!=NULL)
{
prev=curr;
curr=curr->link;
}
if(curr == NULL)
{
cout<<”nThe student with usn “<<usn<<” is not present”;
return;
}
prev->link = curr->link;
writeFile();
display();   //display the records to the screen
}

void main()
{
int ch;
clrscr();
for(;;)
{
cout<<”n 1-Insert_rear n 2-Delete_id n 3-Exit n Enter choice”;
cin>>ch;
switch(ch)
{
case 1:  Insert();
break;
case 2:  Delete();
break;
case 3:  exit(0);
default: cout<<”n Invalid option”;
break;
}
}
}

File Structure ( FS ) Lab Program 11 ( 6th Semester Information Science)

// May 30th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 11 : Write a C++ program to store and retrieve student data from file using hashing. Use any collision resolution technique

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>

//Record specification

class node
{
public: char name[15],usn[15];
node *link;
};

node *h[29];    //Array of record pointers equal to size of hash keys – 29

void insert()
{
char name[15], usn[15], buffer[50];
fstream out;
out.open(“student.txt”,ios::app); //opening student.txt in append mode
if(!out)
{
cout<<”nUnable to open the file in append mode”;
getch();
return;
}

cout<<”nEnter the name = “; cin>>name;
cout<<”n Enter the usn = “; cin>>usn;

strcpy(buffer,name); //Packing the record onto the file using ‘|’ as a delimiter
strcat(buffer,”|”);
strcat(buffer,usn);
strcat(buffer,”n”); // n delimiter for the record
out<<buffer;         // appending the packed record onto the file
out.close();
}

//Insert record into the hash table
void hash_insert(char name1[], char usn1[], int hash_key)
{
node *p,*prev,*curr;
p = new node;       //dynamically allocate space using ‘new’
strcpy(p->name,name1);
strcpy(p->usn,usn1);
p->link=NULL;
prev=NULL;
curr=h[hash_key];
if(curr==NULL)     //getting the hash pointer location Case: No collision
{
h[hash_key]=p;
return;
}
while(curr!=NULL)     // Case : On collision – Insert at rear end
{
prev=curr;
curr=curr->link;
}
prev->link=p;
}

void retrive()
{
fstream in;
char name[15],usn[15];
int j,count;
node *curr;
in.open(“student.txt”,ios::in); // open the record file in input mode
if(!in)
{
cout<<”n Unable to open the file in input mode”;
getch();
exit(0);
}
while(!in.eof())
{
in.getline(name,15,’|');  //unpacking the record
in.getline(usn,15,’n');
count=0;
for(j=0; j<strlen(usn); j++)  //Calculate sum of ascii values of USN
{
count=count+usn[j];
}
count=count%29;               //Hash Key = ASCII count% 29
hash_insert(name,usn,count);
}
cout<<”n Enter the usn =  “;    cin>>usn;
count=0;
for(j=0; j<strlen(usn); j++)        // Calculating Hash Key
count=count+usn[j];
count=count%29;
curr=h[count];
if(curr == NULL)
{
cout<<”nRecord not found”;
getch();
return;
}
do
{
if(strcmp(curr->usn,usn)==0)   //When the record is found, retrieve
{
cout<<”nRecord found : “<<curr->usn<<” “<<curr->name;
getch();
return;
}
else
{
curr=curr->link;
}
}while(curr!=NULL);               //Search till end of list

if(curr==NULL) //End of list reached with no record found
{
cout<<”nRecord not found”;
getch();
return;
}
}

void main()
{
int choice;
clrscr();
fstream out;
out.open(“student.txt”,ios::out);
if(!out)
{
cout<<”nUnable to open the file in out mode”;
getch();
exit(0);
}
for(;;)
{
cout<<”n1:insertn 2: retrive n3:exit nEnter the choice – “;
cin>>choice;
switch(choice)
{
case 1:  insert();
break;
case 2:  retrive();
break;
case 3:  exit(0);
default: cout<<”nInvalid option”;
}
}
}

File Structure ( FS ) Lab Program 10 ( 6th Semester Information Science)

// May 29th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 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();
node* create_node();
void insert(int);
void main_search(int);
void display_tree();
void insert_node(node*,int);
void search(int);
int search_node(node*,int);
int nodefull(node*);
void split(node*);
};

bptree::bptree()
{
leaf=-1;
for(int i=0;i<10;i++)
{
count[i]=-1;
path[i]=-1;
}
}

node* bptree::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 bptree::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 bptree::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<<”n The path traversed is:”;
for(i=0;path[i]!=-1;i++)
cout<<path[i]<<”->”;
if(flag)
cout<<”nnElement found”;
else
cout<<”nnNot Found”;
}

void bptree::display_tree()
{
int i,j,k;
for(i=0;i<=leaf;i++)
{
cout<<”nnLevel——”<<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]<<”t”;
}
}
}

void bptree::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 bptree::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 bptree::nodefull(node* node1)
{
if(node1->ele[3]!=-1)return 1;
else     return 0;
}

void bptree::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<<”tt”<<node1->ele[i];
}

void bptree::split(node *oldnode)
{
node *newnode,*parent,*n1,*n2;
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];
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
t=log(count[leaf]+1)/log(2);
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]];
for(y=0;n2->ele[y]!=-1;y++);
newnode->ele[2]=n2->ele[0];
newnode->child[2]=count[i];
}
}
}
int main()
{
bptree bt;
int choice,key;
while(1)
{
cout<<”nnnMain Menun———–n1:Insertn2:Searchn3:Display treen4:ExitnnEnter your choice :”;
cin>>choice;
switch(choice)
{
case 1:cout<<”nnEnter the element: “;
cin>>key;
bt.insert(key);
break;
case 2: cout<<”nEnter the key: “;
cin>>key;
bt.main_search(key);
break;
case 3: bt.display_tree();
break;
case 4: return 0;
default: cout<<”nnEnter valid Choice”;
}
}
}

File Structure ( FS ) Lab Program 9 ( 6th Semester Information Science)

// May 28th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 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.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>

const int MAX = 4 ;
const int MIN = 2 ;

struct btnode
{
int count;
int value[MAX + 1];
btnode *child[MAX + 1];
};

class btree
{
private : btnode *root ;
public : btree( ) ;
void insert(int val) ;
int setval(int val, btnode *n, int *p, btnode **c);
static btnode *search (int val, btnode *root, int *pos);
static int searchnode(int val, btnode *n, int *pos);
void iskeypresent( int val) ;
void fillnode( int val, btnode *c, btnode *n, int k );
void split( int val, btnode *c, btnode *n, int k, int *y, btnode **newnode );
void clear( btnode *root, int k );
void copysucc( btnode *root, int i );
void merge( int k ) ;
void show( );
static void display( btnode *root );
static void deltree( btnode *root );
~btree( );
};

// initialises data member
btree :: btree( )
{
root=NULL;
}

// searches a key and checks whether it is present or not
void btree :: iskeypresent(int val)
{
int i ;
if( searchnode ( val, root, &i ) )
cout<< “Key found “<<endl;
else
cout<< “Key Not found “<<endl;
}

// inserts a value in the B-tree
void btree :: insert ( int val )
{
int i ;
btnode *c, *n ;
int flag ;
flag = setval( val, root, &i, &c ) ;
if( flag )
{
n = new btnode ;
n -> count = 1 ;
n -> value[1] = i ;
n -> child[0] = root ;
n -> child[1] = c ;
root = n ;
}
}

// sets the value in the node
int btree :: setval ( int val, btnode *n, int *p, btnode **c)
{
int k ;
if ( n == NULL )
{
*p = val ;
*c = NULL ;
return 1 ;
}
else
{
if( searchnode ( val, n, &k ) )
{
cout << endl << “Key value already exists”;
return 0 ;
}
if ( setval ( val, n -> child[k], p, c ) )
{
if ( n -> count < MAX )
{
fillnode ( *p, *c, n, k ) ;
return 0 ;
}
else
{
split ( *p, *c, n, k, p, c ) ;
return 1 ;
}
}
return(0);
}
}

// searches value in the node
btnode * btree :: search ( int val, btnode *root, int *pos )
{
if ( root == NULL ) return NULL ;
else
{
if ( searchnode ( val, root, pos ) )
return root ;
else
return search ( val, root -> child[*pos], pos ) ;
}
}

// searches for the node
int btree :: searchnode ( int val, btnode *n, int *pos )
{
//this condition is used to decide the traversal
if ( val < n -> value[1] )
{
*pos = 0 ;
return 0 ;
}
else
{
*pos = n -> count ;
while ( ( val < n -> value[*pos] ) && *pos > 1 )
(*pos)– ;
if ( val == n -> value[*pos] )
return 1 ;
else
return 0 ;
}
}

// adjusts the value of the node
void btree :: fillnode ( int val, btnode *c, btnode *n, int k )
{
int i ;
for ( i = n -> count ; i > k ; i– )
{
n -> value[i + 1] = n -> value[i] ;
n -> child[i + 1] = n -> child[i] ;
}
n -> value[k + 1] = val ;
n -> child[k + 1] = c ;
n -> count++ ;
}

// splits the node
void btree :: split ( int val, btnode *c, btnode *n, int k, int *y, btnode **newnode )
{
int i, mid ;
if ( k <= MIN )
mid = MIN ;
else
mid = MIN + 1 ;
*newnode = new btnode ;
for ( i = mid + 1 ; i <= MAX ; i++ )
{
( *newnode ) -> value[i - mid] = n -> value[i] ;
( *newnode ) -> child[i - mid] = n -> child[i] ;
}
( *newnode ) -> count = MAX – mid ;
n -> count = mid ;
if ( k <= MIN )
fillnode ( val, c, n, k ) ;
else
fillnode ( val, c, *newnode, k – mid ) ;
*y = n -> value[n -> count] ;
( *newnode ) -> child[0] = n -> child[n -> count] ;
n -> count– ;
}

// removes the value from the node and adjusts the values
void btree :: clear ( btnode *root, int k )
{
int i ;
for ( i = k + 1 ; i <= root -> count ; i++ )
{
root -> value[i - 1] = root -> value[i] ;
root -> child[i - 1] = root -> child[i] ;
}
root -> count– ;
}

// copies the successor of the value that is to be deleted
void btree :: copysucc ( btnode *root, int i )
{
btnode *temp = root -> child[i] ;
while ( temp -> child[0] )
temp = temp -> child[0] ;
root -> value[i] = temp -> value[1] ;
}

// merges two nodes
void btree :: merge ( int k )
{
btnode *temp1, *temp2 ;
temp1 = root -> child[k] ;
temp2 = root -> child[k - 1] ;
temp2 -> count++ ;
temp2 -> value[temp2 -> count] = root -> value[k] ;
temp2 -> child [temp2 -> count] = root -> child[0] ;
for ( int i = 1 ; i <= temp1 -> count ; i++ )
{
temp2 -> count++ ;
temp2 -> value[temp2 -> count] = temp1 -> value[i] ;
temp2 -> child[temp2 -> count] = temp1 -> child[i] ;
}
for ( i = k ; i < root -> count ; i++ )
{
root -> value[i] = root -> value[i + 1] ;
root -> child[i] = root -> child[i + 1] ;
}
root -> count– ;
delete temp1 ;
}

// calls display( )
void btree :: show( )
{
display ( root ) ;
}

// displays the B-tree
void btree :: display ( btnode *root )
{
if ( root != NULL )
{
for ( int i = 0 ; i < root -> count ; i++)
{
display ( root -> child[i] ) ;
cout << root -> value[i + 1] << “t”;
}
display ( root -> child [i] ) ;
}
}

// deallocates memory
void btree :: deltree ( btnode *root )
{
if ( root != NULL )
{
for ( int i = 0 ; i < root -> count ; i++)
{
deltree ( root -> child [i] ) ;
delete ( root -> child[i] ) ;
}
deltree ( root -> child[i] ) ;
delete ( root -> child[i] ) ;
}
}

btree :: ~btree( )
{
deltree ( root ) ;
}

void main( )
{
btree b ;
clrscr();
int num;
int choice=0;
while (choice!=4)
{
cout<<endl<<”l – Insert”<<endl;
cout<<”2 – Search”<<endl;
cout<<”3 – Display”<<endl;
cout<<”4 – Exit”<<endl;
cout<<”Enter your choice”<<endl;
cin>>choice;
switch(choice)
{
case 1:  cout<<”nEnter the elements to be inseretd into b tree n”;
cin>>num;
b.insert ( num ) ;
break;
case 2:  cout<<”nEnter the elements to be searched in b tree n”;
cin>>num;
b.iskeypresent ( num ) ;
break;
case 3:  b.show( ) ;
break;
case 4:  exit(0);
default: cout<<”nInvalid Option n”;
}
}
}

File Structure ( FS ) Lab Program 8 ( 6th Semester Information Science)

// May 28th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 8 :Write a C++ program to read k Lists of names and merge them using kway merge algorithm with k = 8.

#include<iostream.h>
#include<fstream.h>
#include<string.h>

// 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.txtw”,”222.txt”,”llll.txr”};
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;
}

File Structure ( FS ) Lab Program 7 ( 6th Semester Information Science)

// May 27th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 7 : Write a C++ program to read two lists of names and then match the names in the two lists using Cosequential Match based on a single loop. Output the names common to both the lists.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>

void writeLists()
{
fstream out1,out2;
int i,m,n;
char name[20];

out1.open(“file1.txt”,ios::out);
out2.open(“file2.txt”,ios::out);

if( (!out1) || (!out2) )
{
cout<<”Unable to open one of the list files”;
getch();
exit(0);
}

cout<<”Enter no. of names you want to enter in file1″; cin>>m;
cout<<”nEnter the names in ascending ordern”;
for(i-0;i<m;i++)
{
cin>>name;
out1<<name;
out1<<’n';
}

cout<<”Enter no. of names you want to enter in file2″; cin>>n;
cout<<”nEnter the names in ascending order n”;
for(i=0;i<n;i++)
{
cin>>name;
out2<<name;
out2<<’n';
}
out1.close();
out2.close();
}

void main()
{
char list1[100][20], list2[100][20];
int i,j,m,n;
clrscr();
fstream out1,out2,out3;

writeLists();

out1.open(“file1.txt”,ios::in);
out2.open(“file2.txt”,ios::in);
out3.open(“file3.txt”,ios::out);

if( (!out3) || (!out1) || (!out2) )
{
printf(“Unable to open one of the file”);
getch();
exit(0);
}

clrscr();
m=0;
n=0;
while(!out1.eof())
{
out1.getline(list1[m],20,’n');
cout<<list1[m];
m++;
}
while(!out2.eof())
{
out2.getline(list2[n],20,’n');
cout<<list2[n];
n++;
}
m–;
n–;
i=0;
j=0;

cout<<”nElements common to both files are: n “;
while(i<m && j<n)
{
if( strcmp(list1[i],list2[j]) == 0 )
{
out3<<list1[i];
cout<<list1[i]<<”n”;
out3<<’n';
i++;
j++;
}
else if( strcmp(list1[i],list2[j]) < 0 )
i++;
else
j++;
}
getch();
}

File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science)

// May 26th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 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.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>

//using namespace std;

class record
{
public: char age[5];
char usn[20],name[20],branch[5]; char sem[2];
}rec[20],found[20];

char st_no[5],rt_name[20];
int no;

void sort_records()
{
int i,j;
record temp;
for(i=0;i<no-1;i++)
for(j=0;j<no-i-1;j++)
if(strcmp(rec[j].name, rec[j+1].name) > 0)
{
temp=rec[j];
rec[j]=rec[j+1];
rec[j+1]=temp;
}
}

void create_indexfile()
{
fstream index,index2;
int i;
index.open(“secindex.txt”,ios::out);
index2.open(“record.txt”,ios::out);
for(i=0;i<no;i++)
{
index<<rec[i].name<<”|”
<<rec[i].usn<<”|”<<i<<”n”;
index2<<i<<”|”<<rec[i].usn<<”|”<<rec[i].name<<”|”<<rec[i].age<<”|”
<<rec[i].sem<<”|”<<rec[i].branch<<”n”;
}
}

void retrieve_record(char* index)
{
fstream file;
int i;
char ind[2],usn[20],name[20],age[3],sem[3],branch[10];
file.open(“record.txt”,ios::in);
for(i=0;i<no;i++)
{
file.getline(ind,4,’|');
file.getline(usn,20,’|');
file.getline(name,20,’|');
file.getline(age,4,’|');
file.getline(sem,4,’|');
file.getline(branch,5,’n');
if(strcmp(index,ind) == 0)
cout<<”nUSN: “<<usn
<<”nName: “<<name
<<”nAge: “<<age
<<”nSem: “<<sem
<<”nBranch: “<<branch;
}
file.close();
return;
}

void retrieve_details()
{
int k=0,i;
char name[20],usn[20],ind[2];
char chusn[20];
char index[20][20];
fstream file;
file.open(“secindex.txt”,ios::in);
for(i=0;i<no;i++)
{
file.getline(name,20,’|');
file.getline(usn,20,’|');
file.getline(ind,4,’n');
if(strcmp(name,rt_name) == 0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
retrieve_record(index[0]);
return;
}
else
{
cout<<”Please choose the candidate’s USN: n”;
for(i=0;i<k;i++)
cout<<”Name: “<<found[i].name<<” USN: “<<found[i].usn<<endl;
}
cin>>chusn;
for(i=0;i<k;i++)
{
if(strcmp(chusn,found[i].usn) == 0)
{
retrieve_record(index[i]);
return;
}
}
cout<<”Invalid Entry! n”;
return;
}

void delete_record(char indx[])
{
int i;
fstream file1,file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
char index[20][20];
file2.open(“record.txt”,ios::in);
for(i=0;i<no;i++)
{
file2.getline(ind,4,’|');
file2.getline(usn,20,’|');
file2.getline(name,20,’|');
file2.getline(age,5,’|');
file2.getline(sem,5,’|');
file2.getline(branch,8,’n');
strcpy(index[i],ind);
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
{
if(strcmp(index[i],indx) == 0)
flag=i;
}
if(flag==-1)
{
cout<<”Error!n”;
return;
}
if(flag==(no-1))
{
no–;
cout<<”Deleted!n”;
return;
}
for(i=flag;i<no;i++)
{
rec[i]=rec[i+1];
}
no–;
cout<<”Deleted!n”;
file2.close();
file1.open(“secindex.txt”,ios::out);
file2.open(“record.txt”,ios::out);
for(i=0;i<no;i++)
{
file1<<rec[i].name<<”|”<<rec[i].usn<<”|”<<i<<”n”;

file2<<i<<”|”<<rec[i].usn<<”|”<<rec[i].name<<”|”
<<rec[i].age<<”|”<<rec[i].sem<<”|”<<rec[i].branch<<”n”;
}
file1.close();
file2.close();
return;
}

void delete_index(char* nam)
{
fstream file;
int i;
int k=0;
char name[20],usn[20],ind[5],index[20][20],chusn[20];
file.open(“secindex.txt”,ios::in);
for(i=0;i<no;i++)
{
file.getline(name,20,’|');
file.getline(usn,20,’|');
file.getline(ind,4,’n');
if(strcmp(nam,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
delete_record(index[0]);
return;
}
else
{
cout<<”Please choose the candidate’s USN: n”;
for(i=0;i<k;i++)
{
cout<<”Name: “<<found[i].name<<” USN: “<<found[i].usn<<endl;
}
}
cin>>chusn;
for(i=0;i<k;i++)
{
if(strcmp(chusn,found[i].usn)==0)
{
delete_record(index[i]);
return;
}
}
cout<<”Invalid Entry!n”;
return;
}

int main()
{
fstream file1,file2;
int ch;
char rt_usn[20],st_name[20],st_usn[20];
char ind[2],name[20],age[2],sem[5],branch[5];
int i,flag,flag1;
file1.open(“index.txt”,ios::out);
file2.open(“record.txt”,ios::out);
if(!file1 || !file2)
{
cout<<”File creation Error!n”;
exit(0);
}
for(;;)
{
cout<<”nl:Add Recordn 2:Search Record”
<<”n3:Delete Recordn 4:Display Recordn”;
cin>>ch;
switch(ch)
{
case 1: cout<<”Enter the no. of students : “; cin>>no;
cout<<”Enter the details :n”;
for(i=0;i<no;i++)
{
cout<<”nName : “; cin>>rec[i].name;
cout<<”Age : “;    cin>>rec[i].age;
cout<<”USN : “;    cin>>rec[i].usn;
cout<<”Sem : “;    cin>>rec[i].sem;
cout<<”Branch : “; cin>>rec[i].branch;
}
sort_records();
create_indexfile();
file1.close ();
file2.close();
break;

case 2: cout<<”Enter name of the student whose record is to be displayedn”;
cin>>st_name;
file1.open(“secindex.txt”,ios::in);
if(!file1)
{
cout<<”Error !n”;
exit(0);
}
flag1=0;
for(i=0;i<no;i++)
{
file1.getline(rt_name,20,’|');
file1.getline(rt_usn,20,’|');
file1.getline(st_no,4,’n');

if(strcmp(st_name,rt_name)==0)
{
retrieve_details();
flag1=1;
goto one;
}
}
one: if(!flag1)
cout<<”Record search failed !n”;
file1.close();
break;

case 3: cout<<”Enter name of student whose record is to be deletedn”;
cin>>st_name;
file1.open(“secindex.txt”,ios::in);
if(!file1)
{
cout<<”Error !n”;
exit(0);
}
flag=0;
for(i=0;i<no;i++)
{
file1.getline(rt_name,20,’|');
file1.getline(rt_usn,20,’|');
file1.getline(ind,4,’n');

if(strcmp(st_name,rt_name) == 0)
{
delete_index(rt_name);
flag=1;
}
}
if(!flag)
cout<<”Deletion failed !n”;
file1.close();
break;

case 4: for(i=0;i<no;i++)
{
cout<<”nnUSN : “<<rec[i].usn
<<”nName: “<<rec[i].name
<<”nAge : “<<rec[i].age
<<”nSem : “<<rec[i].sem
<<”nBranch : “<<rec[i].branch<<”n”;
}
break;

default: cout<<”Invalid option !n”;
exit(0);
break;
}
}
return 0;
}

File Structure ( FS ) Lab Program 5 ( 6th Semester Information Science)

// April 9th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>

class record
{
public: char age[5],sem[5];
char usn[20], name[20], branch[5];
} rec[20];

char st_no[5];
int no;

void retrieve_details()
{
fstream file2;
char name[20],usn[20],branch[5];
char age[5],sem[5],ind[5];

file2.open(“record.txt”,ios::in);
for(int i=0;i<no;i++)
{
file2.getline(ind,5,’|');
file2.getline(usn,20,’|');
file2.getline(name,20,’|');
file2.getline(age,5,’|');
file2.getline(sem,5,’|');
file2.getline(branch,5,’n');
if(strcmp(ind,st_no)==0)
{
cout<<”nn”<<”Student details: “;
cout<<”n USNttNAMEttAGEttSEMttBRANCHn”;
cout<<”n”<<usn<<”tt”<<name<<”tt”<<age<<”tt”<<sem<<”tt”<<branch<<”n”;
}
}
file2.close();
}

void delete_record(char usno[])
{
int i;
fstream file1,file2;
char age[5],sem[5],name[20],usn[20],branch[5],ind[5];
file2.open(“record.txt”,ios::in);
for(i=0;i<no;i++)
{
file2.getline(ind,5,’|');
file2.getline(usn,20,’|');
file2.getline(name,20,’|');
file2.getline(age,5,’|');
file2.getline(sem,5,’|');
file2.getline(branch,5,’n');
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
{
if(strcmp(rec[i].usn,usno)==0)
flag=i;
}
if(flag==-1)
{
cout<<”Error!!!n”;
return;
}
if(flag==(no-1))
{
no–;
cout<<”Deleted!!!n”;
return;
}
for(i=flag;i<no;i++)
{
rec[i]=rec[i+1];
}
no–;
cout<<”Deleted!!!n”;
file2.close();

file1.open(“index.txt”,ios::out);
file2.open(“record.txt”,ios::out);
for(i=0;i<no;i++)
{
file1<<rec[i].usn<<”|”<<i<<”n”;
file2<<i<<”|”<<rec[i].usn<<”|”<<rec[i].name<<”|”<<rec[i].age<<”|”<<rec[i].sem<<”|”<<rec[i].branch<<”n”;
}
file1.close();
file2.close();
return;
}

int main()
{
fstream file1,file2;
clrscr();
int ch,i,flag,flag1;
char rt_usn[20],st_usn[20];
char ind[5],name[20],age[5],sem[5],branch[5];
file1.open(“index.txt”,ios::out);
file2.open(“record.txt”,ios::out);
if(!file1||!file2)
{
cout<<”File creation Error!n”;
exit(0);
}

for(;;)
{
cout<<”Enter your choice: n 1. Add Recordn 2. Search Recordn 3.Delete Recordn 4.Display recordn”;
cin>>ch;
switch(ch)
{
case 1:cout<<”Enter the no. of studentsn”;
cin>>no;
cout<<”Enter the details :n”;
for(i=0;i<no;i++)
{
cout<<”n Name :”;
cin>>rec[i].name;
cout<<”n Age :”;
cin>>rec[i].age;
cout<<”n USN :”;
cin>>rec[i].usn;
cout<<”n Semester :”;
cin>>rec[i].sem;
cout<<”n Branch :”;
cin>>rec[i].branch;
file1<<rec[i].usn<<”|”<<i<<”n”;
file2<<i<<”|”<<rec[i].usn<<”|”<<rec[i].name<<”|”<<rec[i].age<<”|”<<rec[i].sem<<”|”<<rec[i].branch<<”n”;
}
file1.close();
file2.close();
break;

case 2: cout<<” Enter the usn of the student whose record is to be displayedn”;
cin>>st_usn;
file1.open(“index.txt”,ios::in);
if(!file1)
{
cout<<”Error !!!n”;
exit(0);
}
flag1=0;
for(i=0;i<no;i++)
{
file1.getline(rt_usn,20,’|');
file1.getline(st_no,5,’n');
if(strcmp(st_usn,rt_usn)==0)
{
retrieve_details();
flag1=1;
}
}
if(!flag1)
cout<<”Record not foundn”;
file1.close();
break;
case 3: cout<<” Enter the USN of the student whose record is to be deletedn”;
cin>>st_usn;
file1.open(“index.txt”,ios::in);
if(!file1)
{
cout<<”Error!!!n”;
exit(0);
}
flag=0;
for(i=0;i<no;i++)
{
file1.getline(rt_usn,20,’|');
file1.getline(st_no,5,’n');
if(strcmp(st_usn,rt_usn)==0)
{
delete_record(rt_usn);
flag=1;
}
}
if(!flag)

cout<<”Deletion failedn”;
file1.close();
break;

case 4: cout<<”Student Details :nn”;
cout<<”n USNttNAMEttAGEttSEMttBRANCHn”;
for(i=0;i<no;i++)
{
cout<<”n”<<rec[i].usn<<”tt”<<rec[i].name<<”tt”<<rec[i].age<<”tt”<<rec[i].sem<<”tt”<<rec[i].branch<<”n”;
}
break;
default: cout<<”Invalid Choice!!!!!n”;
exit(0);
break;
}
}
}