I have a binary file that I want to read. I don't really know what the file looks like. However, I do know each message contains a header, some message specific fields, and a termination string which is "DBDBDBDB"
This is what the file contains:
- Header
- marker(uint16_t) this will equal "ST" in the message
- msg_type(uint8_t)
- sequence_id(uint64_t)
- Order Entry Message
- Header
- price(uint64)
- qty(uint32)
(And some other data, but this is the gist of it.)
This file is a data stream from an exchange which contains interaction between different traders, and my job is to decode the stream.
My problem is, I don't know how to display the contents of this binary file so I can't appropriately store the data into a structure.
i've tried the following, and it crashes my cmd window while printing out gibberish.
struct Header
{
uint16_t marker;
uint8_t msg_type;
uint64_t sequence_id;
uint64_t timestamp;
uint8_t msg_direction;
uint16_t msg_len;
};
void parseFile(string filename)
{
streampos size;
Header h;
ifstream file (filename, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
file.read ((char*)&h.marker, sizeof(h.marker));
file.read ((char*)&h.msg_type, sizeof(h.msg_type));
file.read ((char*)&h.sequence_id, sizeof(h.sequence_id));
file.read ((char*)&h.timestamp, sizeof(h.timestamp));
file.read ((char*)&h.msg_direction, sizeof(h.msg_direction));
file.read ((char*)&h.msg_len, sizeof(h.msg_len));
file.close();
}
cout<<h.marker<<endl;
cout<<h.msg_type<<endl;
cout<<h.sequence_id<<endl;
cout<<h.timestamp<<endl;
cout<<h.msg_direction<<endl;
cout<<h.msg_len<<endl;
}
However, the result I am getting is not the ones I am expecting; I am getting
50
16654955463245608
9850698347210351
8
34
I am expecting the first line to be 'ST', and the second line to be either 1,2,3
Also, the protocol of the message is Little Endian, and I am using windows7, which is also little endian.
If anyone can give me pointers or point me to the right direction i would greatly appreciate it.
This is a different question from previously posted question because the previous solution does not work for me, and I am dealing with extra data types.
Much thanks.