c++day-15

Upload: eshamu

Post on 04-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 C++Day-15

    1/12

    [ Binary ]

  • 7/31/2019 C++Day-15

    2/12

    Explain Binary file

    Random access File modes

    Modifying,Deleting,viewing record in a file

  • 7/31/2019 C++Day-15

    3/12

    #include#include#include#include

    class employee{

    private:int eno;char ename[20];float basic,pf,hra,net;

    public:void new_employee();void del_employee();void show_employee();

    void modify_employee();};

    void employee::new_employee() {fstream f

    char wish;int lastno;

  • 7/31/2019 C++Day-15

    4/12

    do{

    f.open("emp.dat",ios::in|ios::binary);f.seekg(0,ios::end);if(f.tellg()==0)lastno=1;else{

    f.seekg(f.tellg()-sizeof(employee),ios::beg);f.read((char *)this,sizeof(employee));

    lastno=eno+1;}f.seekg(0,ios::end);eno=lastno;cout

  • 7/31/2019 C++Day-15

    5/12

    couthra;net=basic+pf+hra;f.write((char *)this,sizeof(employee));f.close();

    coutwish;

    }while(wish=='y');

    }void employee::del_employee(){

    fstream f1,f2;char wish;int no,found;do

    {found=0;f1.open("emp.dat",ios::in);f2.open("temp.dat",ios::out);coutno;

    f1.read((char *)this,sizeof(employee));

  • 7/31/2019 C++Day-15

    6/12

    while(!f1.eof()){if(eno==no)found=1;

    elsef2.write((char *)this,sizeof(employee));f1.read((char *)this,sizeof(employee));}if(!found)cout

  • 7/31/2019 C++Day-15

    7/12

    do{found=0;coutno;

    f.open("emp.dat",ios::in);f.read((char *)this,sizeof(employee));while(!f.eof()) {if(eno==no){found=1;

    cout

  • 7/31/2019 C++Day-15

    8/12

    void employee::modify_employee(){fstream f;int no,found;char wish;

    do{found=0;f.open("emp.dat",ios::in);coutno;

    f.read((char *)this,sizeof(employee));while(!f.eof()){if(eno==no){found=1;

    cout

  • 7/31/2019 C++Day-15

    9/12

    net=basic+pf+hra;f.seekp(f.tellg()-sizeof(employee)),ios::beg;f.write((char *)this,sizeof(employee));}f.read((char *)this,sizeof(employee));

    }if(!found)cout

  • 7/31/2019 C++Day-15

    10/12

    cout

  • 7/31/2019 C++Day-15

    11/12

    To write and read the contents in afile we must create an object

    of class ofstream & ifstream

    The open() function is used to open different files that uses the

    same stream object

    The close() function is used to close a file. It takes no parameters

    and returns no value

    The put() function is used for writing one character at a time in

    a file

    The get() function is used for reading one charater at a time

    from a file

    The write() function is member function of the ofstream class

    which writes text in a line until it encounters a new line character

  • 7/31/2019 C++Day-15

    12/12