I have a .dat file, like this:
NUL NUL NUL ...
So each entry inside this .dat file is a 16-bit signed integer. I want to use C++ to read it two byte 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())
{
//Read file one byte at a time
input.read(&h1, sizeof(short));
}
return pData;
}
catch(int i)
{
return NULL;
}
delete pData;
}
But it gives me error because
input.read(&h1, sizeof(short));
reads a byte at a time. I want to read 2 bytes at a time. Is there anyway I can do this? Or what is the best way to read a .dat file inside which there are a bunch of 16-bit signed int? Thanks
read
will read however many bytes you ask it to read. But you're not putting the data into your pData
array. And you need to cast the first argument to char *
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With