IReadFile* file not reading my struct

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
pararealist
Posts: 6
Joined: Wed Feb 06, 2013 9:10 am
Location: UK

IReadFile* file not reading my struct

Post by pararealist »

Hi,

I have a little problem that is driving me crazy.
I have a struct:

Code: Select all

 
        //! Struct for camera data.
        struct CAMDATA
        {
            vector3df Pos;
            vector3df Rot;
        };      
        CAMDATA *m_camdata;
 
and

Code: Select all

 
m_camdata = new CAMDATA();
 
Then my write and read functions.

Code: Select all

 
//Save camera position data.
void CApplication::saveCameraData()
{
    //get camera position.
    m_camdata[0].Pos = m_camera->getAbsolutePosition();
    m_camdata[0].Rot = getCameraRotation();
 
    IWriteFile* file =
        m_smgr->getFileSystem()->createAndWriteFile( "campos/campos.gsx", false);
    if(file)
    {
        file->write(&m_camdata, sizeof(m_camdata));
        file->drop();
    }
}
 
//Load camera position data.
bool CApplication::loadCameraData()
{
    if( (m_smgr->getFileSystem()->existFile("campos/campos.gsx")) )
    {       
        IReadFile* file = m_smgr->getFileSystem()->createMemoryReadFile((char*)&m_camdata,
                            sizeof(m_camdata), "campos/campos.gsx", false);
 
        if(file)
        {
            long lSize = file->getSize();
 
            file->read(&m_camdata, lSize );
            file->drop();
 
            m_defpos = m_camdata.Pos;
            m_defrot = m_camdata.Rot;
 
            return true;
        }
    }
    return false;
}
 
When i debug the write function camdata contains the pos and rot data.
It writes to file.
When i debug the read function it only reads 0,0,0 for both.

It seems the write function is not writing to the file.
Is there something wrong with the write function and then by default the read one?
Thanks for any help.
pararealist now.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: IReadFile* file not reading my struct

Post by randomMesh »

You only write 4 bytes since sizeof a pointer is 4 on most systems. sizeof does not do what you expect.

Next time try to post a minimal compilable example.
"Whoops..."
pararealist
Posts: 6
Joined: Wed Feb 06, 2013 9:10 am
Location: UK

Re: IReadFile* file not reading my struct

Post by pararealist »

Thanks for your reply.
I could not post full code as these functions are in my application class.
So sizeof is the culprit here?
What is the way to get the correct size then?
I will experiment with pure c++ methods of read writing to file then instead of the irrlicht calls.
pararealist now.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: IReadFile* file not reading my struct

Post by serengeor »

pararealist wrote:So sizeof is the culprit here?
No, what it gave you was what you asked it for, size of a pointer. If you need to know the size of the object use it on an object or class itself.
Working on game: Marrbles (Currently stopped).
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: IReadFile* file not reading my struct

Post by hendu »

Print it out.

printf("sizeof(CAMDATA) = %lu, sizeof(m_camdata) = %lu\n", sizeof(CAMDATA), sizeof(m_camdata))
pararealist
Posts: 6
Joined: Wed Feb 06, 2013 9:10 am
Location: UK

Re: IReadFile* file not reading my struct

Post by pararealist »

These both give the same result.

Code: Select all

 
sizeof(CAMDATA) = 24 , sizeof(m_camdata) = 24
 
I now use oftream and ifstream which writes and reads it correctly.

BUT
i am doing a separate little project to get my head around what you are telling me
as i prefer to use the irrlicht functions again later.

Thank you.
I am 59 :x retired, and programming to keep my brain active stop me becoming senile, so thanks for your patience :) .
pararealist now.
Post Reply