ifstream.read into unsigned char* ?
I'd like to use a certain md5-class. It reads a filestream into an unsigned char array, like this:
Code:
void MD5::update(ifstream& stream)
{
unsigned char buffer[1024];
while (stream.good())
{
stream.read(buffer, 1024);
update(buffer, stream.gcount());
}
}
That code won't compile in Visual C++, because stream.read only accepts a char* and not a unsigned char* as a first parameter.
I noticed that the microsoft implementation uses this:
Code:
typedef basic_ifstream<char, char_traits<char> > ifstream;
So i created this typedef:
Code:
typedef basic_ifstream<unsigned char, char_traits<unsigned char> > ifucstream;
Using 'ifucstream' does compile, but somehow the stream fails after a few loops.
So then i tried using reinterpret_cast to cast buffer as a char* and i also tried 'C'-casting:
Code:
stream.read(reinterpret_cast<char*>(buffer), 1024);
stream.read((char*)buffer, 1024);
Both work fine. However, i've read somewhere that these kind of typecasts are dangerous. Is that true in this case? And does anyone know why my typedef solution doesn't work? And finally, could using reinterpret_cast or 'C'-casting (like above) make the hash calculation slower, and which type of casting should i prefer?