Code: Select all
for (int i=0;i<1;i++){
Client s(44,"Mike","Brown","333333");
ofstream fout("test.bin",ios::binary);
fout.write((char *) (&s),sizeof(Client));
fout.flush();
fout.close();
}
Client s2;
ifstream fin("test.bin",ios::binary);
fin.read((char *) (&s2),sizeof(Client));
cout<<s2.display()<<endl;
fin.close();
Code: Select all
Client s(44,"Mike","Brown","333333");
ofstream fout("test.bin",ios::binary);
fout.write((char *) (&s),sizeof(Client));
fout.flush();
fout.close();
Client s2;
ifstream fin("test.bin",ios::binary);
fin.read((char *) (&s2),sizeof(Client));
cout<<s2.display()<<endl;
fin.close();
after deleting the for loop, the second piece of code runs with no problems. it must be some error caused by the local variable Client s (if you delcare it outside the for loop it works fine)
why is it happening??
and suppose i want to delcare it inside the for loop, how do i fix it??