Thread: stream object

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    stream object

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to give more context since you are using components that do not come from the standard library. For example, what library/framework are you using?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    I use Borland C++ 5, under Windows (want to switch to old DOS, 80186 code) and used object stream objstrm.h...if that's what you asked me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Texture Management in OpenGL
    By Brafil in forum Game Programming
    Replies: 13
    Last Post: 07-16-2009, 04:32 PM
  2. std::string object is overwritten in output stream
    By eatwithaspork in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2009, 03:20 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM