I have been having some issues with trying to read data out of a binary file that is structured so that the header is the first 1024, or 4069 bytes, and the payloads are all regular blocks after that. Below is a model of the code that I have been using:
Header:
#pragma once
#include <stdio.h>
#include <iostream>
#include <fstream>
class BinaryFile
{
public:
long MagicNumber;
long HeaderSize;
long PayloadSize;
//... about 20 other header details
char* Padding; // unused area of the header that is reserved for future use
std::ifstream BinaryFileStream;
BinaryFile();
int Open_Binary_File(const char* path);
int Load_Payload_Into_Buffer(int payload_index, void* buffer);
};
And the C++ code that I have tried to use:
#include "BinaryFile.h"
BinaryFile:BinaryFile()
{
MagicNumber = 0;
HeaderSize = 1024;
PayloadSize = 62830080;
// ... all the other header items, initalized with default values
std:ifstream BinaryFileStream;
Padding[360];
}
int BinaryFile::Open_Binary_File(const char* path) // This is the one that I would like to be using
{
BinaryFileStream.open(path, std::ifstream::binary);
BinaryFileStream.read((char*)&MagicNumber, (size_t) 4);
BinaryFileStream.read((char*)&HeaderSize, (size_t) 4);
// ... The rest of the header is placed into the object
BinaryFileStream.read((char*)&Padding, (size_t) 360);
// The file pointer should now be 1024 bytes into the file, at the end of the header, and at the start of the first payload
return 0;
}
int Load_Payload_Into_Buffer(int payload_index, void* buffer)
{
buffer = malloc(PayloadSize);
size_t offset = HeaderSize + static_cast<long long>(frame_index) * PayloadSize;
BinaryFileStream.seekg(offset, BinaryFileStream.beg);
BinaryFileStream.read((char*)buffer, PayloadSize);
return 0;
Error:
return 1;
}
Below are some variations that I have tried:
int BinaryFile::Open_Binary_File(const char* path)
{
BinaryFileStream.open(path, std::ifstream::binary);
BinaryFileStream.read((char*)&MagicNumber, (size_t) 4);
BinaryFileStream.read((char*)&HeaderSize, (size_t) 4);
// ... The rest of the header is placed into the object
BinaryFileStream.read((char*)&Padding, (size_t) 360);
BinaryFileStream.clear();
BinaryFileStream._Seekbeg.seekg((size_t)0, BinaryFileStream._Seekbeg);
BinaryFileStream.sync();
// The file pointer should now be 0 bytes into the file, at the start of the header
return 0;
}
int BinaryFile::Open_Binary_File(const char* path)
{
BinaryFileStream.open(path, std::ifstream::binary);
BinaryFileStream.read((char*)&MagicNumber, (size_t) 4);
BinaryFileStream.read((char*)&HeaderSize, (size_t) 4);
// ... The rest of the header is placed into the object
BinaryFileStream.read((char*)&Padding, (size_t) 360);
BinaryFileStream.clear();
BinaryFileStream.seekg((size_t)-1024);
BinaryFileStream.sync();
// The file pointer should now be 0 bytes into the file, at the start of the header
return 0;
}
The issue that I am running into to is, the payload that is being returned to the buffer contains some of the next payload. The setting of the file pointer doesn't seem to be working the way that I expect it to. i.e. if I say
BinaryFileStream._Seekbeg.seekg(position, BinaryFileStream._Seekbeg)
I expect the pointer to return to the start of the file, and then seek along the number of bytes that I have said in position. Is there a different way to do this? or is there something that I am missing?
Turns out, what I needed to do was multiply the HeaderSize
by CHAR_BIT
so it would look something like this:
int Load_Payload_Into_Buffer(int payload_index, void* buffer)
{
buffer = malloc(PayloadSize);
size_t offset = HeaderSize*CHAR_BIT + static_cast<long long>(frame_index) * PayloadSize;
BinaryFileStream.seekg(offset, BinaryFileStream.beg);
BinaryFileStream.read((char*)buffer, PayloadSize);
return 0;
Error:
return 1;
}
And what I thought was a portion of the next payload , was in fact the same payload, but split at some arbitrary point, and stacked next to it. So the payload was split approx. 3/4 of the way through, and it was then re-arranged, so that the original last quarter was now at in the first position, and the original first 3/4 was now in the 2nd position.
Hopefully that makes some sense, and will help someone in the future!