Binary file handling in c++. unexpected error! Please help

I was trying to read the data that i wrote to a binary file using structure.
i used struct pointer to read the file but when i read through the file it is showing "the address to my hard disc files" and "Domain";

class retail
{
struct item
{
int sno;
float cost;
char code[25],name[25];
}s1,*s2,*s3;
public:
void write();
void read();
};

void retail::write()
{
fstream writer;
writer.open("Retail_Items.dat",fstream::out|fstream::app|fstream::binary);
s2=(struct item*)malloc(sizeof(item));
cout<<"\nEnter The Serial Number of The item\n";
cin>>s2->sno;
cout<<"\nEnter The Item Name\n";
cin>>s2->name;
cout<<"\nEnter The Iten Code\n";
cin>>s2->code;
cout<<"\nEnter The Item Cost\n";
cin>>s2->cost;
writer.write((char*)s2,sizeof(s2));
// delete[]s2;
writer.close();
}

void retail::read()
{
fstream reader;
reader.open("Retail_Items.dat",fstream::in|fstream::binary);
s2=(struct item*)malloc(sizeof(item));
cout<<"\n________________________________________________"<<endl;
cout<<"S.NO\tITEM_CODE\tITEM_NAME\tCOST"<<endl;
cout<<"________________________________________________"<<endl;
while(reader.read((char*)s2,sizeof(s2)))
{
cout<<s2->sno<<"\t"<<s2->code<<"\t"<<s2->name<<"\t"<<s2->cost<<endl;
}
//delete[] s2; <==========================================================
reader.close();
}

int main()
{
retail a;
a.write();
a.read();
return 0;
}

And can I Deallocate the Memory allocated by pointer the way where i have placed a big arrow(<===)?


Output:


Enter The Serial Number of The item
86

Enter The Item Name
iuiu

Enter The Iten Code
iuuuiiui397

Enter The Item Cost
786874.0

________________________________________________
S.NO ITEM_CODE ITEM_NAME COST
________________________________________________
1 m\AppData\Local\Temp DOMAIN=Mypc 4.86755e-039
86 m\AppData\Local\Temp DOMAIN=Mypc 4.86755e-039


Process returned 0 (0x0) execution time : 11.853 s
Press any key to continue.
Topic archived. No new replies allowed.