Author

Author- Ram Ranjeet Kumar

Wednesday, July 10, 2019

Store Object Data In File In C++


#include<fstream>
#include<iostream>
#include<string.h>
using namespace std;

class item
{
private: int id; char name[20]; float price;
public:
item()
{
id=0;
strcpy(name,"no_name");
price=00.00;
}

void getitemdata()
{
cout<<"Enter Item Id No. Item Name And Price Of Item";
cin>>id;
cin.ignore();
cin.getline(name,19);
cin>>price;
}

void putitemdata()
{
cout<<"\n"<<id<<" "<<name<<" "<<price;
}

int getitem();
};

int item::getitem()
{
if(id==0&&price==00.00)
 {
 cout<<"No Item Records Found";
 return(0);
     }
else
{
ofstream f1;
f1.open("item.dat");
f1.write((char *)this,sizeof(*this));
f1.close();
return(1);
 }
}

int main()
{
item i1,i2;
i1.getitemdata();
i1.putitemdata();
i1.getitem();
i2.putitemdata();
i2.getitem();

}

No comments:

Post a Comment