Wednesday, May 16, 2012

Prog-5


#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 -- print details

  {

                cout<<"\n\n"<<"Student details : ";

 cout<<"\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<<"Deleted !\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 and record data onto

 {                                       //the 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 choice;

 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<<"Please choose :\n1:Add Record          2:Search Record              3:Delete Record     4:Display Record\n";

  cin>>choice;

  switch(choice)

  {

   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<<"Semester : ";

    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<<"Please enter the USN of the student whose record is to be displayed\n";

   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,'|');     //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 the details if index found

     flag1=1;

    }

   }

   if(!flag1)

   cout<<"Record search failed !!\n";

   file1.close();

   break;



   case 3:

   cout<<"Please enter the USN of the student whose record is to be deleted\n";

   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 deletion 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;

                                           //Display all the records

   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 choice !\n";

   exit(0);

   break;

  }

 }

}

No comments:

Post a Comment