Here's what I'm trying to do:
create an empty/NULL char * in one place, pass it to a function who will populate it (and tell me the size), and then use the char * elsewhere. However, when I get out of the function..
Here's the function that expects the char * and attempts to populate it:
bool FileUtilities::loadFullFileBinary( const string &filename, char * fileContents, long & sizeOfContents )
{
FILE *file = fopen(filename.c_str(), "rb");
if ( !file )
returnfalse;
fseek(file, 0, SEEK_END);
sizeOfContents = ftell(file);
cout<<"SIZE:"<<sizeOfContents<<endl;
rewind(file);
fileContents = newchar[sizeOfContents];
if ( fileContents )
{
if ( !fread(fileContents, sizeOfContents, 1, file) == 1 )
{
//puts(buffer);
delete [] fileContents;
sizeOfContents=0;
returnfalse;
}
}
fclose(file);
returntrue;
}
Here's the code (assume in main()) trying to use it.
char * me=NULL;
long size;
bool status = FileUtilities::loadFullFileBinary("test.txt", me, size );
if ( !status )
throw DHException(*this, "Couldn't read file" );
else
{
multiLog.writeLog( "SIZE WAS '%ld'", size );
if ( me != NULL )
{
cout<<"TEST:"<<me[0]<<endl;
cout<<"TEST:"<<me[1]<<endl;
}
else
cout<<"WAS NULL"<<endl;
}
I am finding that the char * is populated inside the function (loadFullFileBinary) but when I attempt to look at it after the function has returned, it is null... What am I doing wrong? (You can ignore the DHException and multiLog bit, they are not related to the problem...)