Re: Returning Char array/pointer? Continuing of thread I am confused
with these concepts.
Thanks for the reply.
I still have one thing:
2. Whne you pass by reference, you are indeed calling malloc (or new)
on the
original pointer (that is what pass by reference does for you).
By say that, did you mean I do not need to call delete in the caller?
Or did you mean I do not need to do the new inside my function?
aDestArr = new char[lngFileSize+1];
--------------------
//Calling the func
char* myByte;
long lngSize = 0;
lngSize = readFileToByteArray("myFileName", myByte);
//Do I need to delete myByte here?
---------------------
my function
long ReadFileToByteArray(char const* aSrcFile, char* aDestArr){
long lngFileSize = 0;
int intBeenRead = 0;
ifstream in;
in.open(aSrcFile, ios::in| ios::binary | ios::ate);
if(in.is_open()||in.bad())
{
lngFileSize = in.tellg();
}else{
throw exception("could not open input file"); // could
not open in
file
return 0;
}
aDestArr = new char[lngFileSize+1];
in.seekg(0,ios::beg);
in.read(aDestArr, lngFileSize);
aDestArr[lngFileSize] = '\0';
in.close();
return lngFileSize;
}
-----------------------------------------------------
I am working on
std:string ReadFileToString( const char* fileName)
My concern is my file is an encrypted file and I open it as binary,
what if it happen contains '\0' in the middle, did the string get
truncated.
Again, thanks
On Feb 11, 2:46 pm, David Wilkinson <no-re...@effisols.com> wrote:
rockdale wrote:
long ReadFileToByteArray(char const* aSrcFile, char*& aDestArr);
Forgive my ignorance, can you show me how to call this function? like
this?
-----------------------------------
char* myByte;
long lngSize = 0;
lngSize = ReadFileToByteArray("myFileName",&myByte);
//process data in myByte
Do I need to delete myByte here? but I never new or malloc memory to
myByte.
------------------------------------
Also, should I do delete inside the function ReadFileToByteArray.
Suppose I am using
char* aDestArr = new char[lngFileSize]; inside the function. I think I=
can not, since I am passing the reference of the array into the func.
--------------------------------------
2 reasons I do not want using std::string. In my appl I still need to
convert the std::string to char array and I want to know more about
this kind of concepts as I am always confused.
Rockdale:
1. When a function uses C++ pass by reference you call it the same as pass=
by
value. In your case:
char* myByte;
long lngSize = 0;
lngSize = ReadFileToByteArray("myFileName", myByte);
2. Whne you pass by reference, you are indeed calling malloc (or new) on t=
he
original pointer (that is what pass by reference does for you). In C the s=
ame
effct can be obtained by passing as char**.
You use malloc() in the function; therefore the caller should use free(). =
You
cannot call free() in the function or the caller cannot retrieve the
information. Again, in a C++ program you should use new[]/delete[] rather =
than
malloc/free.
3. To convert a std::string to a const char* array, use the c_str() method=
..
--
David Wilkinson
Visual C++ MVP- Hide quoted text -
- Show quoted text -