wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
13.Write a C++ program to implement simple index on primary key for a
file of student objects. Implement add ( ), search ( ), delete ( ) using the
index.
#include..iostream.h>
#include..string.h>
#include..fstream.h>
#include..stdlib.h>
//Record specification
class record
{
public: char age[5];
char usn[20],name[20],branch[5];
char sem[2];
}rec[20];
char st_no[5];
int no;
void retrieve_details()
{
fstream file2;
char name[20],usn[20],branch[5];
char ind[5],age[5],sem[5];
file2.open("record.txt",ios::in);
for(int i=0;i..no;i++) //Unpacking record data
{
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) //Required record found- so print details
{
cout..."\n\n"..."Student details are: ";
cout..."\n\nUSN: "...usn..."\nName: "...name..."\nAge: "...age..."\nSem: "...sem..."\nBranch: "...branch..."\n";
}
}
file2.close();
}
void delete_record(char usno[])
{
int i;
fstream file1, file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
file2.open("record.txt",ios::in);
for(i=0;i..no;i++) //Unpack records
{
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++) //Check for the record's existence
{
if(strcmp(rec[i].usn,usno)==0)
flag=i;
}
if(flag==-1) //Record not found
{
cout..."Error !\n";
return;
}
if(flag==(no-1)) //Delete found record
{
no--;
cout..."Deleted !\n";
return;
}
for(i=flag;i..no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout..."\nDeleted !\n";
file2.close();
file1.open("index.txt",ios::out); //Open index and record files
file2.open("record.txt",ios::out); //After deletion
for(i=0;i..no;i++) //Pack index n record data onto files
{
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;
int ch;
char rt_usn[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..."\n1: Add Record"
..."\n2: Search Record"
..."\n3: Delete Record"
..."\n4: Display Record";
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++) //Pack data onto the index and record files
{ //USN is the indexed data
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;
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 USN whose record is to be displayed: ";
cin>>st_usn;
file1.open("index.txt",ios::in);
if(!file1)
{
cout..."\nError !\n";
exit(0);
}
flag1=0;
for(i=0;i..no;i++)
{
file1.getline(rt_usn,20,'|'); //Unpack index file and
file1.getline(st_no,4,'\n'); //look for a match in the USN
if(strcmp(st_usn,rt_usn)==0)
{
retrieve_details(); //Retrieve details if index found
flag1=1;
}
}
if(!flag1)
cout..."Record search failed!\n";
file1.close();
break;
case 3: cout..."Enter USN whose record is to be deleted: ";
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,'|'); //Search index file and
file1.getline(st_no,4,'\n'); //call del if index found
if(strcmp(st_usn, rt_usn)==0)
{
delete_record(rt_usn);
flag=1;
}
}
if(!flag)
cout..."Deletion Failed\n";
file1.close();
break;
case 4: for(i=0;i..no;i++)
{
cout..."\n\n USN: "...rec[i].usn
..."\n Name: "...rec[i].name
..."\n Age: "...rec[i].age
..."\n Sem: "...rec[i].sem
..."\n Branch: "...rec[i].branch;
}
break;
default: cout..."Invalid choice";
exit(0);
break;
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment