TMemFile question

One of my root applications uses non-root binary files with a bunch of root objects serialized using TMemFile: I am running 5.34/36 on Windows 10.

Here’s how I write my root objects to this binary file with FILE pointer *fp:

TMemFile *memfile = new TMemFile("a file name","new"); myObject1->Write() .... myObjectN->Write() memFile->Write(); char *buffer = new char [MAX_SIZE]; Long64_t nBytes = memfile->CopyTo(buffer,MAX_SIZE); fwrite(buffer,(UInt_t)nBytes,1,fp);

When I want to read these object later, I do the following, where fp is the binary FILE * pointer positioned where the TMemFile info starts:

char *buffer = new char [nBytes]; // I know how many bytes there are
fread(buffer,nBytes,1,fp);
TMemFile *memfile = new TMemFile(" a file name",buffer,nBytes);
Iter next(memfile->GetListOfKeys());
while(TKey *key = (TKey*)next())
{
    TObject *pobj = key->ReadObject();
     //do stuff with objects...
}

Everything above works fine - I can retrieve object just fine. What I want to do is the TMemFile equivalent of the following - simply add an new object to an existing TFile

TFile *f = new TFile("file name","UPDATE")
pObj->Write();
f->Write();
delete f;

Here’s what I tried:

char *buffer = new char [50000000];
fread(buffer,nBytes,1,fp);
TMemFile *memfile = new TMemFile("a file name",buffer,nBytes,"UPDATE");
pObj->Write();
memfile->Write();
Long64_t nBytes = memfile->CopyTo(buffer,50000000);
fwrite(buffer,(UInt_t)nBytes,1,fp);
delete [] buffer;

I get no errors, but the new object is not there either… What am I doing wrong? Thanks
Ed

Hi Ed,

This should have worked. I will try to reproduce this failure.

Cheers,
Philippe.

Phillipe,
I made a small stand-alone program for you that demonstated the problem - (un)fortunately it work! Went back to my production code and found the error in my ways: The binary file that I was writing to was opened with read access… Sorry about that…
Ed