Ok so I'm looking for a quick efficient way of reading the following binary data into memory. I'm currently reading it all and then have my own functions that break it up via the separators 0x7d (|) and read each on into a variable, however I was thinking about reading it into a class or a struct perhaps?

Any feedback or suggestions would be appreciated.

46 49 4C 45 48 45 41 44 45 52 8C 00 00 00 30 00 00 00 7C 53 54 52 49 
4E 47 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 7C 03 00 7C 73 74 72 7C 0B 00 
7C 61 6E 6F 74 68 65 72 20 73 74 72 7C 1E 00 00 00 7C 09 00 7C 30 30 
30 2E 34 33 2E 32 39 7C

Break down of the above binary data is as follows;

uchar[0x0a] "46 49 4C 45 48 45 41 44 45 52 "    (this is a constant file header)
ulong[0x4]  "8C 00 00 00"    (unsigned long value)
ulong[0x4]  "30 00 00 00"    (unsigned long value)
uchar[0x1]  "7C"    (field separator)
uchar[0x3f] "53 54 52 49 
4E 47 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00"    (constant char[])
uchar[0x1]  "7C"    (field separator)
ushort[0x2] "03 00"    (length of the next char[] after the field separator)
uchar[0x1]  "7C"    (field separator)
uchar[0x3]  "73 74 72"    (char[] length defined by previous field NOT including the field separator)

the file continues on like this, if its a char[] the pervious field will always contain a short of its length, numeric fields will always be unsigned ong 0x4 bytes etc...

Recommended Answers

All 4 Replies

I don't see '7d' in your data anywhere, did you mean '7c'? What if you read it char-by-char until you hit the '|' char?

Reading into a struct is basically the same as reading into a variable, you just have some extra syntax related to the member access:

struct myData {
  unsigned long field1[4];
}

//...
myData dataObj;
infile >> dataObj.field1[0]
infile >> dataObj.field1[1]
infile >> dataObj.field1[2]
infile >> dataObj.field1[3]

This really isn't the best way to do it, but it demonstrates the concept...

I don't see '7d' in your data anywhere, did you mean '7c'? What if you read it char-by-char until you hit the '|' char?

Reading into a struct is basically the same as reading into a variable, you just have some extra syntax related to the member access:

struct myData {
  unsigned long field1[4];
}

//...
myData dataObj;
infile >> dataObj.field1[0]
infile >> dataObj.field1[1]
infile >> dataObj.field1[2]
infile >> dataObj.field1[3]

This really isn't the best way to do it, but it demonstrates the concept...

Sorry i ment 0x7c my bad, yes I'm currently reading char by char until 0x7c is encountered, is there no way to read the data right into a complete struct something like

struct mydata_t {
unsigned char f1[0x0a];
unsigned long f2[0x4];
...
unsigned shot f3_len[0x2];
unsigned char f3[f3_len];
...
} data;

infile.read(data, sizeof(data));

etc... something along those lines?

Hmmm... You could probably do something w/ an overloaded function or an extraction operator. It's still going to be separate steps to read the data, but when you're ready to read a chunk of data, you'll only have to use the appropriate operator/function.

Hi, I don't know if I understand right, but there is my tip:

You can read the all file, and separate the file in vectors, by tokenize, the file register the data with some separation betwen then? like a blankspace?

Some code are:

std::ifstream inputFile;
std::string line = "data1.dat";
inputFile.open (line.c_str());

while(inputFile.fail() != true ) /* Go through the whole file */
  {
    getline(inputFile,lineFromFile);/* Get a line */
    if(lineFromFile.size() >= 4)/* Check that it's even big enough to contain what we want */
    {                
          std::stringstream h(lineFromFile); //tokenize line//

          h >> currentScanNumber;
          h >> currentScanNumber;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.