#include<fstream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
#define MAXNAME 40
class person
{
private:
                char name[MAXNAME];
                int age;
public:
                void write(ofstream &os)
                {
                                os.write(name,strlen(name));
                                os<<ends;
                                os.write((char*)&age,sizeof(age));
                }
                int read(ifstream &is)
                {
                                is.get(name,MAXNAME,0);
                                name[is.gcount()]=0;
                                is.ignore(1);
                                is.read((char*)&age,sizeof(age));
                                return is.good();
                }
                friend ostream &operator<<(ostream &os,person &b);
                friend istream &operator>>(istream &is,person &b);
};
istream &operator>>(istream &is,person &b)
{
                cout<<"Name :- ";
                is>>ws;
                is.get(b.name,MAXNAME);
                cout<<"Age :- ";
                is>>ws>>b.age;
                return is;
}
ostream &operator<<(ostream &os,person &b)
{
                os<<"Name :- "<<b.name<<endl;
                os<<"Age :- "<<b.age<<endl;
                return os;
}
void main()
{
                clrscr();
                person p_obj;
                int count,obj_id;
                cout<<"Database Creation _ _ _ _ _ "<<endl;
                ofstream ofile("Person.txt",ios::trunc|ios::binary);
                count=0;
                char ch;
                do
                                {
                                cout<<"Enter Object "<<count<<" Details _ _ _ "<<endl;
                                cin>>p_obj;
                                count=count+1;
                                ofile.write((char *) &p_obj,sizeof(p_obj));
                                cout<<"Another ? ";
                                cin>>ch;
                                }while(toupper(ch)=='y' || toupper(ch)=='Y');
                ofile.close();
                fstream iofile("Person.txt",ios::binary|ios::in|ios::out);
                cout<<"Database Access _ _ _ "<<endl;
                while(1)
                                {
                                cout<<"Enter The Object Number To Be Accessed <-1 To End> :- ";
                                cin>>obj_id;
                                if(obj_id<0 || obj_id>=count)
                                                break;
                                int location=obj_id*sizeof(p_obj);
                                iofile.seekg(location,ios::beg);
                                iofile.read((char*) &p_obj,sizeof(p_obj));
                                cout<<p_obj;
                                cout<<"Wants To Modify ? ";
                                cin>>ch;
                                if(ch=='y' || ch=='Y')
                                                {
                                                cin>>p_obj;
                                                iofile.seekp(location,ios::beg);
                                                iofile.write((char*) &p_obj,sizeof(p_obj));
                                                }
                                }
                iofile.close();
                getch();
}

 

Advertisements