CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2009
    Posts
    12

    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

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Erroneous cast between character types

    I am curious why you would choose to create an ofstream with new. That seems a bit odd.

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured