Reading a Binary file in C++
<p>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"</p>

<p>This is what the file contains:</p>

<ul>
<li>Header

<ul>
<li>marker(uint16_t) this will equal "ST" in the message</li>
<li>msg_type(uint8_t)</li>
<li>sequence_id(uint64_t) </li>
</ul></li>
<li>Order Entry Message

<ul>
<li>Header</li>
<li>price(uint64)</li>
<li>qty(uint32)</li>
</ul></li>
</ul>

<p>(And some other data, but this is the gist of it.)</p>

<p>This file is a data stream from an exchange which contains interaction between different traders, and my job is to decode the stream.</p>

<p>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. </p>

<p>i've tried the following, and it crashes my cmd window while printing out gibberish.</p>

<pre><code>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*)&amp;h.marker, sizeof(h.marker));
      file.read ((char*)&amp;h.msg_type, sizeof(h.msg_type));
      file.read ((char*)&amp;h.sequence_id, sizeof(h.sequence_id));
      file.read ((char*)&amp;h.timestamp, sizeof(h.timestamp));
      file.read ((char*)&amp;h.msg_direction, sizeof(h.msg_direction));
      file.read ((char*)&amp;h.msg_len, sizeof(h.msg_len));
      file.close();
    }
  cout&lt;&lt;h.marker&lt;&lt;endl;
  cout&lt;&lt;h.msg_type&lt;&lt;endl;
  cout&lt;&lt;h.sequence_id&lt;&lt;endl;
  cout&lt;&lt;h.timestamp&lt;&lt;endl;
  cout&lt;&lt;h.msg_direction&lt;&lt;endl;
  cout&lt;&lt;h.msg_len&lt;&lt;endl;

}
</code></pre>

<p>However, the result I am getting is not the ones I am expecting; I am getting</p>

<pre><code>50

16654955463245608
9850698347210351
8
34
</code></pre>

<p>I am expecting the first line to be 'ST', and the second line to be either 1,2,3</p>

<p>Also, the protocol of the message is Little Endian, and I am using windows7, which is also little endian.</p>

<p>If anyone can give me pointers or point me to the right direction i would greatly appreciate it.</p>

<p>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.</p>

<p>Much thanks.</p>