I have a .dat file, for example:
NUL NUL NUL ...
Thus, each entry inside this .dat file is a 16-bit signed integer. I want to use C ++ to read two bytes at a time. This is currently my code to read it
short* ReadData(char* fileName, long imgWidth, long imgHeight, long bytePerPixel)
{
short * pData = new short[imgWidth*imgHeight*bytePerPixel];
short h1;
try
{
std::ifstream input(fileName, std::ios::binary);
while(!input.eof())
{
input.read(&h1, sizeof(short));
}
return pData;
}
catch(int i)
{
return NULL;
}
delete pData;
}
But that gives me an error because
input.read(&h1, sizeof(short));
reads a byte at a time. I want to read 2 bytes at a time. Anyway, can I do this? Or what is the best way to read a .dat file inside which there is a bunch of 16-bit signed int? Thanks
source
share