Mar 15, 2012 at 11:13am Mar 15, 2012 at 11:13am UTC
Hi.
I have a question about serialize and desarialize dynamic table of object's.
Let's say I have a class CTowar_internal:
class CTowar_internal
{
int a;
int b;
}
...
now I have two funcitions responsible for serialize and desiarialze a table of CTwoar_internal objects:
void Serialize::save(CTowar_internal* tw, int size)
{
ofstream ofs("data.ros", ios::out);
char* buff = new char[sizeof(CTowar_internal) * size];
memcpy(buff,tw,sizeof(CTowar_internal) * size);
ofs.write(buff, sizeof(CTowar_internal) * size);
ofs.flush();
ofs.close();
}
void Serialize::read(CTowar_internal* tw, int size)
{
ifstream ifs("data.ros", ios::in);
char* buff = new char[sizeof(CTowar_internal) * size];
ifs.read((char *)buff, sizeof(CTowar_internal) * size);
memcpy(tw,buff,sizeof(CTowar_internal) * size);
ifs.close();
}
Now let's use that functions:
main()
{
CTowar_internal* tw = new CTowar_internal[3];
// filling fields:
for(int i=0;i<3;i++)
{
tw[i].a = i;
tw[i].b = i+1;
}
//saving table :
save(tw,3);
//now let's create a new table and load saved objects:
CTowar_internal* tw2 = new CTowar_internal[3];
read(tw2,3);
...
And the tw2 table doesn't match tw table. The tw2 table is loaded with trash.
Can anybody tell me why it doesn't work ?