while(entrada.read((char*)&c,sizeof(Contacto)))
// ^^^^^^^^^
If% co_of% results from using, directly or indirectly, dynamic memory that reading will fail you. why? Because then you will copy pointers instead of the values that interest you.
An example:
class Prueba {
int* datos;
};
int main ()
{
std::cout << sizeof(Prueba);
}
The following program will print 4 or 8 depending on whether you compile it in 32 bits or 64 ... regardless of what you have stored in Contacto
.
If you take and dump datos
to a file so rough, what you're going to do is save only the value of the pointer ... so if Prueba
points to a list of 20 elements located in datos
, 0x102030 will be stored in the file. Note that the data is not being saved, then when the program is closed the data will be lost forever ...
and why does the program crack?
Because you are overwriting the pointers causing them to point to invalid positions.
If, in the previous example, you read the file and save its contents in an object of 0x102030
, what you will get is that Prueba
points to datos
... it is easy to guess that memory does not it belongs to your application, so the operating system protects and kills your application with the sole purpose of preventing it from corrupting the memory.
What you have to do is save the members of your object manually ... one by one ... the mechanism you describe is only valid if you avoid dynamic memory:
struct Funciona
{
int var1;
char var2 [100];
};
struct NoFunciona
{
int var1;
char *var2; // un puntero!!!!
std::string var3; // string usa internamente punteros!!!
};
Since you did not put the declaration of 0x102030
it is impossible to give you more information