how to correctly write vector to binary file in c++?

You are writing to file the vector structure, not its data buffer. Try change writing procedure to:

ofstream fout("data.dat", ios::out | ios::binary);
fout.write((char*)&student[0], student.size() * sizeof(Student));
fout.close();

And instead of calculation size of vector from file size, it’s better write vector size (number of objects) before. In the case you can write to the same file other data.

size_t size = student.size();
fout.write((char*)&size, sizeof(size));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)