RSS Forums RSS
Please support our C++ advertiser: Programming Forums

Passing char * to function and populating inside

Join Date: Feb 2005
Posts: 464
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Passing char * to function and populating inside

  #1  
Jul 27th, 2005
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...)
AddThis Social Bookmark Button
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:31 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC