User Tools

Site Tools


c_-_c_files:class_to_a_file

C - C++ Files - Class to a file

#include<iostream>
#include<fstream>
using namespace std;
 
class item
{
private:
  int itno,stock;
  char itname[10];
  float pprice,sprice;
 
public:
  void getitem()
  {
    cout<<"Enter the item no:";
    cin>>itno;
    cout<<"Enter the item name:";
    cin>>itname;
    cout<<"Enter the purchase price:";
    cin>>pprice;
    cout<<"Enter the selling price:";
    cin>>sprice;
    cout<<"Enter current stock:";
    cin>>stock;
  }
};
 
 
int main()
{
  fstream f;
  item i;
  char wish;
  f.open("item.dat",ios::app);
 
  do
  {
    i.getitem();
    f.write((char *)&i,(sizeof(i)));
    cout<<"Wish to add another item(y/n):";
    cin>>wish;
  } while(wish=='y');
 
  f.close();
 
  return 0;
}
c_-_c_files/class_to_a_file.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki