Enter your email address & Win:

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

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

-->

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 Record\n 2:Search Record”
<<”\n3:Delete Record\n 4:Display Record\n”;
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 displayed\n”;
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 deleted\n”;
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<<”\n\nUSN : “<<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;
}


If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. File Structure ( FS ) Lab Program 5 ( 6th Semester Information Science)
  2. File Structure ( FS ) Lab Program 8 ( 6th Semester Information Science)
  3. File Structure ( FS ) Lab Program 4 ( 6th Semester Information Science)
  4. File Structure ( FS ) Lab Program 2 ( 6th Semester Information Science)
  5. File Structure ( FS ) Lab Program 3 ( 6th Semester Information Science)
  • shaardula
    This program was said to be given in SirMVIT for the first time.Works fine but has a logical error "even if you give same usns twice it accepts",this is entirely against the priciple of primary keys.there can be 2 secondary keys of same name but not primary keys.either modify the program or even better we wrote the progs ourself!
  • Dear Shaardula,
    I don't know where the program was given first time, one of my blog reader had sent it to me.

    You just blabber that you have writtent the programs, but unfortunately never share it !

    Anyway I'm publishing two alternatives for the above program :

    1. http://www.pingsense.com/2009/06/file-structure...

    2. http://www.pingsense.com/2009/06/file-structure...


    Readers can choose which one they want !
  • shaardula
    This program was said to be given in SirMVIT for the first time.Works fine but has a logical error "even if you give same usns twice it accepts",this is entirely against the priciple of primary keys.there can be 2 secondary keys of same name but not primary keys.either modify the program or even better we wrote the progs ourself!
  • Dear Shaardula,
    I don't know where the program was given first time, one of my blog reader had sent it to me.

    You just blabber that you have writtent the programs, but unfortunately never share it !

    Anyway I'm publishing two alternatives for the above program :

    1. http://www.pingsense.com/2009/06/file-structure...

    2. http://www.pingsense.com/2009/06/file-structure...


    Readers can choose which one they want !
blog comments powered by Disqus
Get Adobe Flash playerPlugin by wpburn.com wordpress themes