-
June 25th, 2009, 02:58 PM
#1
Erroneous cast between character types
Hi all,
I'm experiencing an issue that is related to converting a project from VC 6.0 to VC 2003.
In VC 6.0, the project used fstream.h. So, in order to use VC 2003, I changed the include to <fstream>.
This caused a few compile time errors. Here is one:
Code:
BYTE *Buffer = NULL;
int Length = 0;
// fill Buffer with data and update Length accordingly.
ofstream *fp3 = new ofstream(Path,ios::binary);
fp3->write(Buffer,Length);
error C2664: 'std::basic_ostream<_Elem,_Traits>::write' : cannot convert parameter 1 from 'BYTE *' to 'const char *'
This makes sense, BYTE is really an unsigned character, and the compiler cannot implicitly convert an unsigned character to a signed one. (I'm assuming that the fact that this is an error implies that char == signed char).
So, in my ignorance, I changed the last line to
Code:
fp3->write((char*) Buffer, Length);
and went on my merry way.
Well, I just tracked down a bug to this cast (or a similar one involving read()).
Presumably, these Buffers contain weird characters (that are represented by negative integers, in the signed case, or integers larger than 128 in the unsigned case), and simply casting them is screwing things up.
I'm not sure how to proceed from here. There are a ton of functions that pass BYTEs back and forth, so I can't simply change the Buffer to a signed character buffer.
Am I understanding the problem correctly?
Any suggestions on how I should resolve this issue?
Please let me know if any further explanation would be helpful.
Regards,
Joe
-
June 25th, 2009, 03:13 PM
#2
Re: Erroneous cast between character types
Casting just tells the compiler to treat the variable as a different type, it doesn't affect the actual value(s) held in the variable. If you have incorrect characters in the file, they were incorrect in the array (in other words, the casting isn't causing this problem).
Viggy
-
June 25th, 2009, 04:11 PM
#3
Re: Erroneous cast between character types
I am curious why you would choose to create an ofstream with new. That seems a bit odd.
-
June 26th, 2009, 02:24 AM
#4
Re: Erroneous cast between character types
Code:
BYTE *Buffer = NULL;
int Length = 0;
// fill Buffer with data and update Length accordingly.
ofstream *fp3 = new ofstream(Path,ios::binary);
fp3->write(Buffer,Length);
What are the contents of Buffer and Length?
The contents of 'Buffer' need not to be string. So, don't do string testing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|