Question about type conversion and casting

Discussion in 'C++' started by seia0106, Jul 28, 2003.

  1. seia0106

    seia0106 Guest

    Hello
    I am writing a function to read a binary file. Here is a part of code
    #include <fstream>
    ..
    ..
    BYTE *pData;
    long lDataLen;
    pms->GetPointer(&pData);
    lDataLen = pms->GetSize();
    // Read one line at a time till end of file..
    if (m_inFile.getline(pData,
    lDataLen))pms->SetActualDataLength(strlen((char*)pData)+1);
    else {return S_FALSE;}
    ...................
    The error message that i get is this

    error C2664: 'class std::basic_istream<char,struct
    std::char_traits<char> > &__thiscall std::basic_istream<char,struct
    std::char_traits<char> >::getline(char *,int)'
    : cannot convert parameter 1 from 'unsigned char *' to 'char *'
    Types pointed to are unrelated; conversion requires
    reinterpret_cast, C-style cast or function-style cast

    Conflict in datatypes is causing this error. How can i solve this
    problem? Which type of casting is better here and how it should be
    used here.
    thanks
     
    seia0106, Jul 28, 2003
    #1
    1. Advertising

  2. "seia0106" <> wrote in message
    news:...
    > Hello
    > I am writing a function to read a binary file. Here is a part of code
    > #include <fstream>
    > .
    > .
    > BYTE *pData;
    > long lDataLen;
    > pms->GetPointer(&pData);
    > lDataLen = pms->GetSize();
    > // Read one line at a time till end of file..
    > if (m_inFile.getline(pData,
    > lDataLen))pms->SetActualDataLength(strlen((char*)pData)+1);
    > else {return S_FALSE;}
    > ..................
    > The error message that i get is this
    >
    > error C2664: 'class std::basic_istream<char,struct
    > std::char_traits<char> > &__thiscall std::basic_istream<char,struct
    > std::char_traits<char> >::getline(char *,int)'
    > : cannot convert parameter 1 from 'unsigned char *' to 'char *'
    > Types pointed to are unrelated; conversion requires
    > reinterpret_cast, C-style cast or function-style cast
    >
    > Conflict in datatypes is causing this error. How can i solve this
    > problem? Which type of casting is better here and how it should be
    > used here.
    > thanks


    Why are you trying getline in a binary file? getline is for text files.

    Anyway you can cast pData to the required type, one of the few common
    instances where a cast is justified.

    if (m_inFile.getline((char*)pData,lDataLen))

    But the fact that you cast pData to (char*) twice, suggests that maybe you
    would do better to declare is as char* in the first place. After all it does
    appear to be text data, and char* is used for text.

    john
     
    John Harrison, Jul 28, 2003
    #2
    1. Advertising

  3. seia0106

    seia0106 Guest

    Thank you for the reply.
    The file is a text file. I have tried to do it the way you said but it
    is still not working. In this function *pData must be declared as a
    'BYTE'type. So how to use a 'BYTE' type *pData as a first parameter of
    getline() method and in strlen()? The error is same that
    "cannot convert parameter 1 from 'const char ** ' to 'unsigned char **
    '"

    I have looked into the text books for unsigned char type, char type
    and signed char type but I am little confused about their differences
    and uses.

    thanks in advance


    "John Harrison" <> wrote in message news:<bg3nk7$kp9sp$-berlin.de>...
    > "seia0106" <> wrote in message
    > news:...
    > > Hello
    > > I am writing a function to read a binary file. Here is a part of code
    > > #include <fstream>
    > > .
    > > .
    > > BYTE *pData;
    > > long lDataLen;
    > > pms->GetPointer(&pData);
    > > lDataLen = pms->GetSize();
    > > // Read one line at a time till end of file..
    > > if (m_inFile.getline(pData,
    > > lDataLen))pms->SetActualDataLength(strlen((char*)pData)+1);
    > > else {return S_FALSE;}
    > > ..................
    > > The error message that i get is this
    > >
    > > error C2664: 'class std::basic_istream<char,struct
    > > std::char_traits<char> > &__thiscall std::basic_istream<char,struct
    > > std::char_traits<char> >::getline(char *,int)'
    > > : cannot convert parameter 1 from 'unsigned char *' to 'char *'
    > > Types pointed to are unrelated; conversion requires
    > > reinterpret_cast, C-style cast or function-style cast
    > >
    > > Conflict in datatypes is causing this error. How can i solve this
    > > problem? Which type of casting is better here and how it should be
    > > used here.
    > > thanks

    >
    > Why are you trying getline in a binary file? getline is for text files.
    >
    > Anyway you can cast pData to the required type, one of the few common
    > instances where a cast is justified.
    >
    > if (m_inFile.getline((char*)pData,lDataLen))
    >
    > But the fact that you cast pData to (char*) twice, suggests that maybe you
    > would do better to declare is as char* in the first place. After all it does
    > appear to be text data, and char* is used for text.
    >
    > john
     
    seia0106, Jul 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,816
    Andrew McDonagh
    Jan 8, 2005
  2. heyo
    Replies:
    3
    Views:
    939
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    809
    Dan Pop
    Apr 2, 2004
  4. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    577
    James Kanze
    Dec 20, 2009
  5. Shannon
    Replies:
    10
    Views:
    1,050
    Jonathan Bromley
    Oct 22, 2010
Loading...

Share This Page