Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GLubyte and read problem

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I have the following code
Code:
  /* open file */
  fstream  file (filename, ios::in | ios::binary);

if (!file)
{
   PRINT_ERROR("Cannot open file (%s)\n",filename);
} 
  
  file.seekg (0, ios::beg);
  /* read dimensions */
  file.read(&hi,1);
  file.read(&lo,1);
  lenx = hi*256+lo;
  file.read(&hi,1);
  file.read(&lo,1);
  leny = hi*256+lo;
  file.read(&hi,1);
  file.read(&lo,1);
  lenz = hi*256+lo;
 
   pixels = new GLubyte [lenx * leny * lenz];

if (pixels==NULL)
{
   PRINT_ERROR("Read_data() : Out of memory (%s)\n",filename);
}
 /*read data*/
 file.read(pixels,lenx * leny * lenz);

lenx etc are GLints.

I get an error when compiling "invalid conversion Glubyte* to char*

How can I solve this?

Thanks!
 
Lemme guess, you have
Code:
char *pixels;
and not
Code:
Glubyte *pixels;

The fix is to fix the type of your variable, or fix the type in the new call.

--
 
no I do have Glubyte *pixels in .h
 
How about actually saying which line the error message is on.

Perhaps it's complaining about this line then
file.read(pixels,lenx * leny * lenz);



--
 
that's correct sorry for not mentioning it but the error indeed points to that line file.read(pixels,lenx * leny * lenz); I found many examples using similar statements but in C using fread, since I write in C++ I thought I would use read instead...
 
In which case, you probably want something like this

Code:
file.read( reinterpret_cast<char*>( pixels ), lenx * leny * lenz);

--
 
Thanks Salem this has solved one problem. By the way why do we need to use reinterpret_cast in C++ not in C?

My other problem is that hi and lo variables are declared char but should be unsigned char (I get lenx = -128) Do I need to use reintepret_cast as well for the first few reads
like
Code:
file.read(&hi,1);
 
> By the way why do we need to use reinterpret_cast in C++ not in C?
In C, fread() is usually declared as taking void* as the first parameter. As such, you can pass a pointer to any type of memory and C nicely converts it to a void* pointer without any more fuss.

C++ on the other hand has no such automatic type conversion, so it wouldn't matter what type file.read() had, you're pretty much guaranteed to need some casting. The fact that the default type is char* doesn't help much either, since the most useful type for unspecified blocks of memory is unsigned char.

> Do I need to use reintepret_cast as well for the first few reads
Yes, that would be the thing to do.

--
 
Salem,

Thanks for your explanation re void* in C it is clear now.

I noticed that by casting with (char*) works equally well. no need for reinterpret_cast then?
 
There are several variations of C++ cast expressions which are applicable in different circumstances, and which offer a degree of safety.

Simply using C-style casts in a C++ program might be legal, but it's hardly using the language in the way it was intended, or to its best effect.

C-style casts are brute-force, all bets are off kind of things, and you really don't want to be using them when alternatives exist.

--
 
so you are saying that using
Code:
file.read( reinterpret_cast<char*>( pixels ), lenx * leny * lenz);
is better than
Code:
file.read( (char*) pixels , lenx * leny * lenz);

then?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top