In the following code, the object is not being written into file in line 30 stfile.write((char*)this,sizeof(item));
When line 26 : stfile.read((char*)&ob,sizeof(item));
is hidden the object is written into the file.
Please help!!!
fstream stfile;
class item
{
int code;
char name[25];
int price;
int qty;
public:
int getcode()
{
return code;
}
void GetNew()
{
// cout<<"\nENTER THE CODE"; //Unhide this when
// cin>>code; //entering first iteminto the file
cout<<"\n ENTER THE ITEM NAME";
gets(name);
cout<<"\n ENTER THE PRICE";
cin>>price;
cout<<"\n ENTER STOCK";
cin>>qty;
item ob;
stfile.seekg(-1*sizeof(item),ios::end);
stfile.read((char*)&ob,sizeof(item));
code=ob.getcode();code++; //hide these three
stfile.seekp(0,ios::end); //lines when entering
cout<<stfile.tellp()<<endl; //first iten into the file
stfile.write((char*)this,sizeof(item));
cout<<stfile.tellp();
return ;
}
void show()
{ cout<<"\ncode " <<code
<<"\nname "
<<name
<<"\n price"
<<price
<<"\n quantity"
<<qty;
}
};
void ShowItem(int c)
{
stfile.seekg(0,ios::beg);
stfile.clear();
while(!stfile.eof())
{item ob;
stfile.read((char*)&ob,sizeof(item));
if(stfile.eof())
{perror("search"); break;}
if(ob.getcode()==c)
{ ob.show();break; }
}
}