Question about type conversion and casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seia0106

    Question about type conversion and casting

    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(&pD ata);
    lDataLen = pms->GetSize();
    // Read one line at a time till end of file..
    if (m_inFile.getli ne(pData,
    lDataLen))pms->SetActualDataL ength(strlen((c har*)pData)+1);
    else {return S_FALSE;}
    ............... ....
    The error message that i get is this

    error C2664: 'class std::basic_istr eam<char,struct
    std::char_trait s<char> > &__thiscall std::basic_istr eam<char,struct
    std::char_trait s<char> >::getline(ch ar *,int)'
    : cannot convert parameter 1 from 'unsigned char *' to 'char *'
    Types pointed to are unrelated; conversion requires
    reinterpret_cas t, 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
  • John Harrison

    #2
    Re: Question about type conversion and casting


    "seia0106" <miahmed67@yaho o.com> wrote in message
    news:4fe296bd.0 307280854.7e7fa 0cb@posting.goo gle.com...[color=blue]
    > 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(&pD ata);
    > lDataLen = pms->GetSize();
    > // Read one line at a time till end of file..
    > if (m_inFile.getli ne(pData,
    > lDataLen))pms->SetActualDataL ength(strlen((c har*)pData)+1);
    > else {return S_FALSE;}
    > ............... ...
    > The error message that i get is this
    >
    > error C2664: 'class std::basic_istr eam<char,struct
    > std::char_trait s<char> > &__thiscall std::basic_istr eam<char,struct
    > std::char_trait s<char> >::getline(ch ar *,int)'
    > : cannot convert parameter 1 from 'unsigned char *' to 'char *'
    > Types pointed to are unrelated; conversion requires
    > reinterpret_cas t, 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[/color]

    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.getli ne((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



    Comment

    • seia0106

      #3
      Re: Question about type conversion and casting

      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" <john_andronicu s@hotmail.com> wrote in message news:<bg3nk7$kp 9sp$1@ID-196037.news.uni-berlin.de>...[color=blue]
      > "seia0106" <miahmed67@yaho o.com> wrote in message
      > news:4fe296bd.0 307280854.7e7fa 0cb@posting.goo gle.com...[color=green]
      > > 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(&pD ata);
      > > lDataLen = pms->GetSize();
      > > // Read one line at a time till end of file..
      > > if (m_inFile.getli ne(pData,
      > > lDataLen))pms->SetActualDataL ength(strlen((c har*)pData)+1);
      > > else {return S_FALSE;}
      > > ............... ...
      > > The error message that i get is this
      > >
      > > error C2664: 'class std::basic_istr eam<char,struct
      > > std::char_trait s<char> > &__thiscall std::basic_istr eam<char,struct
      > > std::char_trait s<char> >::getline(ch ar *,int)'
      > > : cannot convert parameter 1 from 'unsigned char *' to 'char *'
      > > Types pointed to are unrelated; conversion requires
      > > reinterpret_cas t, 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[/color]
      >
      > 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.getli ne((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[/color]

      Comment

      Working...