Hello, rather new to C++, but I need a solution to stream an object in C++.
I tried the following code, and wanted to store the object dt in memory buffer, and read it back into newdt.
But nothing changed in the newdt.
Wat did I do wrong?
Code:
class DataClass : public TStreamableBase {
public:
int a;
int b;
DataClass(){};
private:
DECLARE_STREAMABLE( , DataClass,1);
};
void* DataClass::Streamer::Read(ipstream& in, uint32) const
{
ReadBaseObject(static_cast<DataClass*>(GetObject()), in);
in >> GetObject()->a >> GetObject()->b;
return GetObject();
}
void DataClass::Streamer::Write(opstream& out) const
{
WriteBaseObject(static_cast<DataClass*>(GetObject()), out);
out << GetObject()->a << GetObject()->b;
}
IMPLEMENT_STREAMABLE ( DataClass );
void main(){
//FILE *fp;
char * buffer[100];
int stop;
DataClass dt;
dt.a=100;
dt.b=200;
DataClass newdt;
newdt.a=1;
newdt.b=2;
printf(" Initial Contence of the buffer newtd is: %d, %d \n",newdt.a,newdt.b);
streambuf bf((char *)(&newdt), sizeof(DataClass));
ptbf = &bf;
opstream outpstr(ptbf);
ipstream inpstr(ptbf);
ModuleId mid = GetModuleId();
outpstr.writeObject(&dt, 1, mid);
inpstr.readBytes(&newdt, sizeof(DataClass));
printf(" Contence of the buffer newtd is: %d, %d \n",newdt.a,newdt.b);
}
Thanks