Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question about type conversion and casting

Reply
Thread Tools

Question about type conversion and casting

 
 
seia0106
Guest
Posts: n/a
 
      07-28-2003
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-28-2003

"seia0106" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
 
seia0106
Guest
Posts: n/a
 
      07-29-2003
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" <(E-Mail Removed)> wrote in message news:<bg3nk7$kp9sp$(E-Mail Removed)-berlin.de>...
> "seia0106" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type casting / conversion again Shannon VHDL 10 10-22-2010 11:54 AM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM
Casting between const type** and type** Kevin L C++ 6 08-11-2003 03:39 PM



Advertisments