I have a a binary file format with a bunch of headers and floating point data. I am working on a code that parses the binary file. Reading the headers was not hard but when I tried to read the data I ran into some difficulties.
I opened the file and read the headers as the following:
ifs.open(fileName, std::ifstream::in | std::ifstream::binary);
char textHeader[3200];
BinaryHeader binaryHeader;
ifs.read(textHeader,sizeof(textHeader));
ifs.read(reinterpret_cast<char *>(&binaryHeader), sizeof(binaryHeader));
The documentation says the data is stored as: 4-byte IBM floating-point and I tried something similar:
vector<float> readData(int sampleSize){
float tmp;
std::vector<float> tmpVector;
for (int i = 0; i<sampleSize; i++){
ifs.read(reinterpret_cast<char *>(&tmp), sizeof(tmp));
std::cout << tmp << std::endl;
tmpVector.push_back(tmp);
}
return tmpVector;
}
Sadly the result does not seem correct. What do I do wrong?
EDIT: Forgot to mention, the binary data is in big-endian, but if I print the tmp values out the data does not seem correct either way.
Conclusion: The 4-byte IBM floating-point is not the same as the float.
readData()
function you create a temporary vector on that functions stack frame and you then return it. Maybe try changing the signature of this function to accept anstd::vector<float>
by reference and pass it into the function instead of return a copy to a temporary. – Francis Cuglerfloat
. If that’s the case You’ll have to do some work to translate the input into something your hardware can work with. – Pete Becker