Hi all, hope this title was descriptive enough. I'm a bit new to binary files so if I'm not explaining things properly please let me know.
Basically, I have a type of data file that is stored in binary format. The data file consists of a large header file, followed by a time series of voltage and current values. The software that writes the data from the equipment to the binary file also has a method for converting the binary file to a plain text file. Unfortunately, the plain text files are large and unwieldy. Therefore, I'd like to be able to open the binary file myself and process the data.
The problem I'm having is that it seems like if you don't know the type of data a sequence of bits is supposed to represent, it's difficult to convert the bits to a usable form.
For example, if I try to read in a certain number of bits into type
char
and then output the
char
s, the result I get is this:
1 2 3 4 5 6
|
A
B
F
2 //ABF2 is the type of this binary file--this part makes sense
//unintelligible ASCII symbols follow
|
As I understand it, this is because only the first 4 bytes have a sensible
char
representation; the rest of the bit sequence may consist of any number of data types.
So, to tackle the problem I've defined a template function that will read in a particular data type T's worth of bits, and output the result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
template<typename T>
void readElement(std::ifstream& inputstream)
{
T t = 0;
if(inputstream.is_open())
{
inputstream.read((char*)&t, sizeof(T));
std::cerr << "t = " << t << "\n";
}
else
{
std::cerr << "Couldn't open file!\n";
}
return t;
}
|
So I'm trying to treat this like a guessing game, basically read in a sequence of bits assuming they are supposed to represent a particular data type, and if something sensible comes out, that means I've figured out a certain part of the header.
Finally, my questions: Is this a worthwhile pursuit? Is it possible to figure out the format of the file by proceeding in this way? Is there a better way of figuring out how I should read the file?
Thanks!