My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program to enter the values in student database

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<fstream.h>
void menu();
int a;
fstream f,temp;
void openin();
void openapp();


class student
{
    char name[20],email[20],roll[20],fee[20];
    public:
    void getdata();
    void show();
    int compair(char[]);
};

void student::getdata()
{
    cout<<"\n enter roll no.:->  ";
    gets(roll);
    cout<<"\n enter name:->  ";
    gets(name);
    cout<<"\n Enter email:->  ";
    gets(email);
    cout<<"\n Enter fee:-> ";
    gets(fee);
}

void student::show()
{
    cout<<"  roll no is:->  " << roll<<endl;
    cout<<"    name is:->  "<<name<<endl;
    cout<<"    email is:->  "<<email<<endl;
    cout<<"    fee is:->  "<<fee<<endl<<endl<<endl;
}

int student::compair(char g[20])
{
    if(strcmp(g,roll)==0)
    return (1);
    else
    return (0);
}


void menu()
{
   clrscr();
   cout<<" (1) Insert new recored"<<endl;
   cout<<" (2) Show all record"<<endl;
   cout<<" (3) search a record by roll no."<<endl;
   cout<<" (4) delete a record"<<endl;
   cout<<" (5) modify a record"<<endl;
   cout<<" (6) exit"<<endl;
   cout<<"     Enter ur choise........";
   cin>>a;
}

void main()
{
   student s,s1;


   do
   {    menu();
    if(a==1)
    {     s.getdata();
          openapp();
           if(f.fail())
           {  cout<<"file could not b opened";
          getch();
          break;
           }

          f.write((char*)&s,sizeof(s));
          f.close();
    }

    if(a==2)
    {     openin();
           if(f.fail())
           {  cout<<"file could not b opened";
          getch();
          break;
           }
          // int k=0;
          while(1)
          {  //  f.seekg((k*sizeof(s)),ios::beg);
           f.read((char*)&s,sizeof(s));
           if(f.eof())
           break;
           s.show();
          // k++;
          }
          getch();
          f.close();
    }

    if(a==3)
    {     char r[20];
          int i,k;
          i=k=0;
          cout<<"\n\n    enter the roll no.: ";
          gets(r);
          openin();
          if(f.fail())
          {  cout<<"file could not b opened";
          getch();
          break;
          }
          while(1)
          {     f.seekg((i*sizeof(s)),ios::beg);
            f.read((char*)&s,sizeof(s));
            if(f.eof())
            break;
            int m=s.compair(r);
            if(m==1)
            { k++;
              s.show();
              getch();
              break;
            }
            i++;
          }
          f.close();
          if(k==0)
          {     cout<<"\n\n\n      NO RECORD FOUND";
            getch();
          }
     }

  /*    if(a==4)
    {     char r[20];
          int i,k;
          i=k=0;
          cout<<"\n\n    enter the roll no.: ";
          gets(r);
          openin();
          if(f.fail())
          {  cout<<"file could not b opened";
          getch();
          break;
          }
          while(1)
          {     f.seekg((i*sizeof(s)),ios::beg);
            f.read((char*)&s,sizeof(s));
            if(f.eof())
            break;
            int m=s.compair(r);
            if(m==1)
            {   k++;
            cout<<"your previous record is: "<<endl;
            s.show();
            getch();
            f.seekg(0,ios::cur);
            int h=f.tellg();
            int n=h/sizeof(s);
            temp.open("c:/temp.bin",ios::app|ios::binary);
            if(temp.fail())
            {  cout<<"file could not b opened";
               getch();
               break;

            }
            for(int t=0;i<n;i++)
            {   f.seekg((t*sizeof(s)),ios::beg);
                f.read((char*)&s,sizeof(s));
                temp.write((char*)&s1,sizeof(s1));
             }
            for(t=n+1;;t++)
            {   if(f.eof())
                break;
                f.seekg((t*sizeof(s)),ios::beg);
                f.read((char*)&s,sizeof(s));
                temp.write((char*)&s1,sizeof(s1));
            }
            f.close();
            temp.close();
            f.open("c:/upin.bin",ios::out|ios::binary);
            temp.open("c:/temp.bin",ios::in|ios::binary);
            t=0;
            while(1)
            {   if(temp.eof())
                break;
                temp.seekg((t*sizeof(s1)),ios::beg);
                temp.read((char*)&s1,sizeof(s1));
                f.write((char*)&s,sizeof(s));
                t++;
             }
             f.close();
             temp.close();
             break;
           }
           i++;
          }
          f.close();
          if(k==0)
          {     cout<<"\n\n\n      NO RECORD FOUND";
            getch();
          }

    }          */

    if(a==5)
    {     char r[20];
          int i,k;
          i=k=0;
          cout<<"\n\n    enter the roll no.: ";
          gets(r);
          f.open("c:/upin2.bin",ios::in|ios::binary);
          if(f.fail())
          {  cout<<"file could not b opened";
          getch();
          break;
          }
          while(1)
          {     f.seekp((i*sizeof(s)),ios::beg);
            f.read((char*)&s,sizeof(s));
            if(f.eof())
            break;
            int m=s.compair(r);
            if(m==1)
            { k++;
              cout<<"Your previous record is:"<<endl<<endl;
              s.show();
              f.close();
              cout<<"\n\nNow modify the record:"<<endl;
              s.getdata();
              f.open("c:/upin2.bin",ios::ate|ios::binary);
              f.seekp((i*sizeof(s)),ios::beg);
              f.write((char*)&s,sizeof(s));
              cout<<"record is successfully modified";
              f.close();
              getch();
              break;
            }
            i++;
          }
          f.close();
          if(k==0)
          {     cout<<"\n\n\n      NO RECORD FOUND";
            getch();
          }

    }

    if(a==6)
    exit(0);

    }while(a!=6);
}
   void openin()
   {    f.open("c:/upin2.bin",ios::in|ios::binary);
   }

   void openapp()
   {    f.open("c:/upin2.bin",ios::app|ios::binary);
   }

0 comments: