replacing unsigned char * with CString
Okay, for some reason this one has me stumped. I am trying to replace all
of my char * (and unsigned char *) with CString's so to (hopefully) clean up
the code and make it much easier to track down, fix, and avoid errors (such
as not releasing memory, or not allocating enough memory, etc.).
My problem has arising when I try to read a lot of (essentially) random
characters form a text file using the CFile::Read operator. I am going to
show both version of my code below (totally stripped down), hopefully
someone can tell me how to use the CString properly in this case (or if I
cannot use a CString what it is I should be using):
Example 1 (which works):
CFile imageFile;
unsigned char * tempImage = new unsigned char [15000];
imageFile.Open("USE.jpg", CFile::typeBinary | CFile::modeRead, NULL);
radarMapSize = imageFile.GetLength();
imageFile.Read(tempImage , radarMapSize);
imageFile.Close();
//Read data into the radarMapDataArray structure
for (int i=0; i<int(radarMapSize/63)+1; i++) {
for (int p=i*63; p<i*63+63;p++) {
int gh = (p%63);
radarMapDataArray[i][p%63]=tempImage[p];
}
}
(please ignore the fact I took out my error checking (around opening the
file opening for example)...it is the 3rd last line that concerns me
(radarMapDataArray[i]p%63]=tempImage[p];
When I rewrite this to use the CString it looks like so:
CFile imageFile;
CString imageString;
imageFile.Open("USE.jpg", CFile::typeBinary | CFile::modeRead, NULL);
radarMapSize = imageFile.GetLength();
imageFile.Read(tempImage.GetBuffer(radarMapSize), radarMapSize);
tempImage.ReleaseBuffer();
imageFile.Close();
//Read data into the radarMapDataArray structure
for (int i=0; i<int(radarMapSize/63)+1; i++) {
for (int p=i*63; p<i*63+63;p++) {
int gh = (p%63);
radarMapDataArray[i][p%63]=tempImage.GetAt(p);
}
}
and that (what I though was) equivalent line
radarMapDataArray[i][p%63]=tempImage.GetAt(p); will now crash. This is
happening (I believe) because the file I am reading contains extended ASCII
characters. So of course that line crashes at different points depending on
how many characters the CString actually contains (for example right now
when I put in a breakpoint it only looks like the CString tempImage has 8
characters).
So I think I understand what is happening (the CString is only 8 characters
long so my call to GetAt() is trying to access memory that isn't allocated
yet) but what I don't understand is how to make the two programs above
equal. I have tried putting a cast operation before my call to
tempImage.GetBuffer(radarMapSize) (I put (unsigned char *)) and this
compiles and runs, but (naturally) doesn't do anything to fix the problem.
(Note - I also put a checkpoint in the first section of the code and my
unsigned char * LOOKS to contain the same data as the CString would (only
the 8 characters) however it's call using pointers [] doesn't crash whereas
my GetAt() does :S
Can someone please point out to me what in the world I am doing wrong...I
must be missing something??
Thanks,
Arlis T. Rose