KK posted:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cas t<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */
ifstream fp("in.bin",ios ::binary);
char buff[4];
fp.read(buff,4) ;
unsigned int loadSize = Byte2Int(buff);
Thank you.
KK
You don't specify the amount of bits in a byte, however, looking at your
code, we can make an educated guess of 8.
You don't specify the amount of bytes in an int, however, looking at your
code, we can make an educated guess of 4.
You don't specify the byte order of the integer stored in the file, so we
can only hope that it's the same as the system's.
You don't specify the negative number system used to represent the number
in the file, so we can only hope that it's the same as the system's.
You don't specify whether the integer in the file contains padding bits, or
where they're located, nor do you specify whether the system stores
integers with padding bits, or where they're located.
Working with the scraps being given, try this:
unsigned Func( char (&array)[4] )
{
return reinterpret_cas t<int&>( array );
}
--
Frederick Gotham