ifstream::read(unsigned char*, int) 
Author Message
 ifstream::read(unsigned char*, int)

Hi,
The following code:

  std::ifstream file(fileName);
  if( !file )
    return 0;

  int imageSize = width*height*components;
  unsigned char* pixelBuf = new unsigned char[imageSize];
  file.read(pixelBuf, imageSize);
  file.close();

throws the the following compiler error:
error C2664: 'read' : cannot convert parameter 1 from 'unsigned char *' to
'char *'

Does ifstream not have access to istream::read(unsigned char*, int)?

Thanks,
John



Thu, 13 May 2004 09:35:45 GMT  
 ifstream::read(unsigned char*, int)

Quote:

> Does ifstream not have access to istream::read(unsigned char*, int)?

Nope, because such a member function doesn't exist.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



Fri, 14 May 2004 23:04:39 GMT  
 ifstream::read(unsigned char*, int)
Thanks, I see from the online documentation that it takes char_type. I must
have been reading old documentation from MSDN:

<snip>
 istream::read
istream& read( char* pch, int nCount );
istream& read( unsigned char* puch, int nCount );

istream& read( signed char* psch, int nCount );

Parameters

pch, puch, psch

A pointer to a character array.

nCount

The maximum number of characters to read.

Remarks

Extracts bytes from the stream until the limit nCount is reached or until
the end of file is reached. The read function is useful for binary stream
input.

</snip>

Anyways, so guess I need to contruct the ifstream with a character_traits
specifying unsigned int ?

John.



Quote:

> > Does ifstream not have access to istream::read(unsigned char*, int)?

> Nope, because such a member function doesn't exist.

> P.J. Plauger
> Dinkumware, Ltd.
> http://www.dinkumware.com



Sat, 15 May 2004 03:48:05 GMT  
 ifstream::read(unsigned char*, int)


Quote:
> Anyways, so guess I need to contruct the ifstream with a character_traits
> specifying unsigned int ?

Supposed to read unsigned char.


Sat, 15 May 2004 03:51:20 GMT  
 ifstream::read(unsigned char*, int)

Quote:



>> Anyways, so guess I need to contruct the ifstream with a character_traits
>> specifying unsigned int ?

>Supposed to read unsigned char.

Don't do that - it isn't the intention of the char_type parameter -
this is there to allow you to add support for custom character types.
This takes a fair amount of work compared to doing this:

mystream.read(reinterpret_cast<char*>(myunsignedcharptr), n);

Tom



Sat, 15 May 2004 07:46:11 GMT  
 ifstream::read(unsigned char*, int)
Well, I think I won't use the C++ file streams then, I'll use the C standard
library file functions. I'm surprised that the C++ file streams don't
support unsigned char types, is there a good reason for this?

John.


Quote:



> >> Anyways, so guess I need to contruct the ifstream with a
character_traits
> >> specifying unsigned int ?

> >Supposed to read unsigned char.

> Don't do that - it isn't the intention of the char_type parameter -
> this is there to allow you to add support for custom character types.
> This takes a fair amount of work compared to doing this:

> mystream.read(reinterpret_cast<char*>(myunsignedcharptr), n);

> Tom



Sat, 15 May 2004 08:35:22 GMT  
 ifstream::read(unsigned char*, int)

Quote:

> Well, I think I won't use the C++ file streams then, I'll use the C standard
> library file functions. I'm surprised that the C++ file streams don't
> support unsigned char types, is there a good reason for this?

The C standard library functions don't have any unsigned char support
either. All the functions either act on plain char or on void*. There
happens to be an implicit cast from unsigned char* to void*, hence the
appearance of unsigned char support in fread.

There is no reason to support unsigned char directly when by
supporting plain char you get unsigned char support for free with a
simple cast. Supporting it directly would lead to quite a lot of
completely unnecessary code and interface bloat.

Still, the C standard IO functions work just as well for binary IO as
the C++ ones, and are part of C++ too. But I don't think choosing
between them on the basis of requiring an explicit cast on one and not
on the other is really valid reasoning.

Tom



Sat, 15 May 2004 21:57:13 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. bytes to unsigned char, unsigned short, unsigned int, ...

2. From unsigned int to unsigned char

3. unsigned char assignment to unsigned int.

4. *unsigned int = unsigned int bit: 1;

5. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

6. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

7. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

8. design decision, unsigned char or int interface

9. Converting int to unsigned char *.

10. char * in network byte order to unsigned long int

11. unsigned char --> int conversion

12. casting signed char to unsigned int

 

 
Powered by phpBB® Forum Software