0

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.

  • You want to read a binary file but you *don't* have the format? – Borgleader Jun 10 '16 at 14:52
  • I must be missing something here. Do i NEED to know the format beforehand? Can't I just print out the file and see what it looks it? –  Jun 10 '16 at 14:53
  • Use `myfile.read()` to read binary data. – πάντα ῥεῖ Jun 10 '16 at 14:54
  • @weizeng *Can't I just print out the file and see what it looks it?* Open it in notepad and tell me if what you see makes any sense. – Borgleader Jun 10 '16 at 14:56
  • Of course not. But shouldn't the binary reader option in fstream convert the binary code into something readable? –  Jun 10 '16 at 14:57
  • 2
    @weizeng Binary reader? theres no such thing, if youre talking about the binary flag thats related to \r\n conversion. It doesnt interpret the data any more than that. – Borgleader Jun 10 '16 at 15:00
  • @weizeng Binary mode only inhibits conversion of newline characters on platforms where that is necessary. (A slight exaggeration; there are platforms where the difference is significant, but the probability of you using such a machine is zero.) – molbdnilo Jun 10 '16 at 15:01
  • Ok so what do i do here guys, im at a loss.... I've also added extra info in the question –  Jun 10 '16 at 15:04
  • 1
    What's wrong with [`fstream::read`](http://www.cplusplus.com/reference/istream/istream/read/) ? – Jabberwocky Jun 10 '16 at 15:09
  • I recommend moving the reading functionality into the class, something like `binary_read(std::istream& input);`. This allows you to declare the variables as private and employ the concepts of *data hiding* and *encapsulation*. – Thomas Matthews Jun 10 '16 at 15:37
  • @Thomas, but first I need to figure out how this binary file works and how to store it into a structure –  Jun 10 '16 at 15:40
  • Hey guys I still haven't found my solution yet. Can someone help? –  Jun 10 '16 at 16:16

1 Answers1

2

The problem you're seeing right now is that istream::read wants a pointer to char, and you're passing some other type. You can get this much of the code working by taking an address and casting it:

file.read ((char *)&h.marker, sizeof(h.marker));

Then you need to add similar read calls for the other fields, of course (but it is good that you're adding one thing, getting it to work, then moving on to more).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Ok, Im getting closer, it populates the struct, but not with what I was expecting. I'm going to update my post with my results. –  Jun 10 '16 at 15:28
  • I did file.read ((char *)&h.marker, sizeof(h.marker)); The output shows that its a smiley face or other random integers... –  Jun 10 '16 at 16:17