c++ - Reading a binary file incrementally -


i made question parsing binary file, , sort of got work here.

https://stackoverflow.com/questions/37755225/need-help-reading-binary-file-to-a-structure?noredirect=1#comment62983158_37755225

but face new challenge , in need of help, please bare me.

my binary file looks much longer...

st.........¸.°Ý.ø...0.œ...........esz4 1975..........iyh.testdbdbdbdbst...........°Ý.ø...................dbdbdbdbst.........p.´Ý.ø...0.œ...........esz4 1975..........htc.testdbdbdbdbst.........‹‚´Ý.ø...................dbdbdbdbst.........ƒd.Þ.ø...0.œ...........esz4 1975..........arm.testdbdbdbdbst.........«e.Þ.ø...................dbdbdbdb

basically, every message starts 'st' , ends 'dbdbdbdb'. goal parse each message , store message data structure. in addition, every message different depending on type, , different type of message have additional members.

the problem having is, have no idea how iterate through binary file... if regular file, can while(getline(file,s)), binary file?? there way say, find first "st" , "dbdbdbdb" , parse middle stuff, move on next set of st , db? or somehow read file incrementally keeping track of am?

i apologise ahead of time posting code.

#pragma pack(push, 1) struct header {     uint16_t marker;     uint8_t msg_type;     uint64_t sequence_id;     uint64_t timestamp;     uint8_t msg_direction;     uint16_t msg_len; }; #pragma pack(pop) struct orderentrymessage {     header header;     uint64_t price;     uint32_t qty;     char instrument[10];     uint8_t side;     uint64_t client_assigned_id;     uint8_t time_in_force;     char trader_tag[3];     uint8_t firm_id;     char firm[256] ;      char termination_string[8];  };  struct acknowledgementmessage {     header header;     uint32_t order_id;     uint64_t client_id;     uint8_t order_status;     uint8_t reject_code;     char termination_string[8]; };  struct orderfillmessage {     header header;     uint32_t order_id;     uint64_t fill_price;     uint32_t fill_qty;     uint8_t no_of_contras;     uint8_t firm_id;     char trader_tag[3];     uint32_t qty;     char termination_string[8];  }; void tradedecoder::createmessage() {     ifstream file("example_data_file.bin", std::ios::binary);       //i want somehow loop here keep looking headers st      header h;     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();      switch(h.sequence_id)     {         case 1:              createorderentrymessage(h); //this methods creates orderentrymessage header         break;         case 2:             createorderackmessage(h); //same above         break;         case 3:             createorderfillmessage(h); //same above         break;         default:         break;     } }   

much thanks.....

you can read whole file buffer , parse buffer according requirements.

i use

fread  

to read whole file buffer , process/parse buffer byte byte.

this example:

/* fread - read entire file buffer */ #include <stdio.h> #include <stdlib.h>  int main () {   file * pfile;   long lsize;   char * buffer;   size_t result;    pfile = fopen ( "myfile.bin" , "rb" );   if (pfile==null)       {fputs ("file error",stderr); exit (-1);}    // obtain file size:   fseek (pfile , 0 , seek_end);   lsize = ftell (pfile);   rewind (pfile);    // allocate memory contain whole file:   buffer = (char*) malloc (sizeof(char)*lsize);   if (buffer == null) // malloc failed      {fputs ("memory error",stderr); exit (-2);}    // copy file buffer:   result = fread (buffer,1,lsize,pfile);   if (result != lsize)      {fputs ("reading error",stderr); exit (-3);}    // whole file loaded in memory buffer.    // can process whole buffer now:     // (int i=0; i<lsize;i++)   // {   //    processbufferbytebybyte(buffer[i]);   // }   // or   // processbuffer(buffer,lsize);    // terminate   fclose (pfile);   free (buffer);   return 0; } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -