I have a structure with 2 integers and I want to save them in a binary file and read it again.
Here is my code:
static const char *ADMIN_FILE = "admin.bin"; struct pw { int a; int b; }; void main(){ pw* p = new pw(); pw* q = new pw(); std::ofstream fout(ADMIN_FILE, ios_base::out | ios_base::binary | ios_base::trunc); std::ifstream fin(ADMIN_FILE, ios_base::in | ios_base::binary); p->a=123; p->b=321; fout.write((const char*)p, sizeof(pw)); fin.read((char*)q, sizeof(pw)); fin.close(); cout << q->a << endl; }
The output I get is 0 . Can someone tell me what the problem is?
blaxc source share