WHat is wrong here?

Hi!

I am using a CMemFile and CArchive like this;

bool CTest::save(CMemFile *_f) {

unsigned char *buf = NULL;

_f->write((void*)buf,(UINT)sizebuf);

return true;
}

Then somewhere else I try to read the content of that CMemFile through CArchive (this is only a simplified code)

CArchive ar(&mf,CArchive::load);

if(sizeBuf){
for (int i = 0; i < sizeBuf; i++) {
ar >> *(buffer+i);
}
}

This is not the right way to get the unsigned char* from the CArchive. What am I doing wrong here?

Thx
[617 byte] By [laitinen] at [2007-11-20 1:11:16]
# 1 Re: WHat is wrong here?
I cannot answer your question, but maybe someone else can in a forum related to MFC. This is a Non Visual C++ forum.
Eisenbart at 2007-11-10 23:19:12 >
# 2 Re: WHat is wrong here?
Oh, the lovely CArchive. I remember posting in the VC++ forum about that. With the non-const-correct Serialize.

Of course to allow your object to be serialised you have to remember to stick some horrible macros into your code.

And of course it clashes with standard streaming, so you can't mix your non-MFC objects in with it.
NMTop40 at 2007-11-10 23:20:12 >
# 3 Re: WHat is wrong here?
Memory for buffer doesn't appear to be allocated. What does writeToBuff do? What is sizebuf? Where does it get set?
GCDEF at 2007-11-10 23:21:17 >
# 4 Re: WHat is wrong here?
In addition to what GCDEF wrote...
If you somewhere (within writeToBuff - ?) allocate the memory for buffer - then you erroneously don't free this memory.
VictorN at 2007-11-10 23:22:11 >
# 5 Re: WHat is wrong here?
Why are you even trying to use a CArchive to read when you didn't use one to write?
GCDEF at 2007-11-10 23:23:16 >
# 6 Re: WHat is wrong here?
[ merged threads ]

Regards,
Siddhartha
Siddhartha at 2007-11-10 23:24:15 >
# 7 Re: WHat is wrong here?
I am sorry for being uncomplete in my code. This is a more exactly code;

bool CTest::save(CMemFile *_f) {

unsigned char *buf = NULL;

//img is a representation of an image

savetiftobuffer(&buf,&img,0);

_f->write((void*)buf,(UINT)sizebuf);

return true;
}

Then somewhere else I try to read the content of that CMemFile through CArchive (this is only a simplified code)

CArchive ar(&mf,CArchive::load);

//sizeBuf is the size of the buffer, got this value just above this
//code snippet

unsigned char *buffer = new unsigned char[sizeBuf];

for (int i = 0; i < sizeBuf; i++) {
ar >> *(buffer+i);
}


This is not the right way to get the unsigned char* from the CArchive. What am I doing wrong here?

Should i get it more like this;

ar>>buffer;

The problem is anyway that the correct data doesnt come through!

Thanks for your input!
laitinen at 2007-11-10 23:25:19 >
# 8 Re: WHat is wrong here?
Again, why are you using a CArchive to read when you didn't use it to write?
GCDEF at 2007-11-10 23:26:17 >
# 9 Re: WHat is wrong here?
I used the CArchive to write some data, basically CString variables. These data have already been succesfully read. Remember, this is only an extract of the code, and above the code you can see I write to the CArchive as well!

So anyone with any tips?

Thanks for commenting!
laitinen at 2007-11-10 23:27:26 >