Help with binary file reading

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1099 Views - Last Post: 24 April 2016 - 01:19 PM Rate Topic: -----

#1 csnook   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 23-April 16

Help with binary file reading

Posted 23 April 2016 - 04:27 PM

I am trying to read a header binary file and output the contents of the file. I know what the output should look like and am getting a portion of the correct data but the rest is outputting as characters or other garbage.

The output should be in the following format...

Version: 4
Code: 0 - OK
Total Length: 95
Service Type: 2 - High Priority
Grumpy: 1
Sneezy: 0
Bashful: 1
Doc: 0
Dopey: 0
Tipsey: 0
Surley: 1
SnowWhite: 1
Hop Count: 42
Source Address: 222.222.222.222
Dest Address: 4.3.2.1
Data: Some data....

This is what I have so far but have just hit a wall and need a little push in the right direction. See attached pic for my output and below for code.

Attached Image

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

// header struct definition
struct s_header {
	unsigned char verCode;
	unsigned char serviceType;
	unsigned short totLength;
	unsigned char reserved;
	unsigned char flags;
	unsigned char codes;
	//unsigned char services;
	unsigned short hopCount;
	unsigned long srcAddr;
	unsigned long destAddr;
} fileheader;// you can make a global header variable RIGHT HERE if you want to

// string tables
char * codes[] = {
	"OK",
	"Possible data corruption"
	"Reserved"
};

char * services[] = {
	"Normal",
	"Low Priority",
	"High Priority",
	"Medium Priority",
	"So-so Priority",
	"Needed Yesterday",
	"Nevermind"
	"Reserved"
};

char * flags[] = {
	"Grumpy",
	"Sneezy",
	"Bashful",
	"Doc",
	"Dopey",
	"Tipsy",
	"Surly",
	"Snow White"
};

int main(int argc, char * argv[]) {
	char buffer[1024];
	char ch, code;
	int x;

	ifstream myfile("PACKETS.txt", ios::in | ios::binary);
	if (!myfile) {
		cout << "Cannot open file.\n";
		return 1;
	}
	
	myfile.read((char *)& fileheader, sizeof s_header);

	fileheader.verCode = fileheader.verCode >> 4;
    cout << "Version: " << setw(8) << (int)fileheader.verCode << endl;
	cout << "Code: " << setw(11) << (int) fileheader.verCode << " - ";
	code = fileheader.codes;
	for (int i = 0; i < sizeof(fileheader.verCode); i++) {
		cout << codes[i] << endl;
	}

	cout << "Total Length: " << setw(4) << fileheader.totLength << endl;
	cout << "Service Type: " << setw(3) << (int)fileheader.serviceType << " - ";
	cout << services[fileheader.serviceType] << endl;
	
	x = fileheader.flags;
	for (int i = 0; i < 8; i++) {
		printf("  %-7s:\t%d\n", flags[i], x & 1);
		x = x >> 1;
	}
	
	cout << "Hop Count: " << fileheader.hopCount << endl;	
	cout << "Source Address: " << fileheader.srcAddr << endl;
	cout << "Dest Address: " << fileheader.destAddr << endl;	
	cout << "Data: ";

	myfile.close();
	cout << endl;
	system("pause");
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help with binary file reading

#2 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Help with binary file reading

Posted 23 April 2016 - 04:54 PM

I don't have time to get into the details but I suggest that you check sizeof(fileheader) and compare that to the sum of the sizes of the individual fields in the struct. I suspect that there may be some padding in there since the struct seems to contain an odd number of bytes.

It would probably be helpful if you would post a sample input file for people to test with.
Was This Post Helpful? 2
  • +
  • -

#3 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Help with binary file reading

Posted 23 April 2016 - 05:02 PM

Another thing that makes me wonder is that you're reading it as a binary file but calling it a .txt file. A text file contains newline chars and in some os's CR chars as well.
Was This Post Helpful? 0
  • +
  • -

#4 csnook   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 23-April 16

Re: Help with binary file reading

Posted 23 April 2016 - 05:07 PM

I will post the file supplied

Attached File  PACKETS.txt (382bytes)
Number of downloads: 19<--- File used for source code above. The issue here is I was given a brief overview of 1) structs and 2) binary file I/O. We never covered padding (#pragma) or anything associated with bitwise. Basically what I generated is what I have been able to teach myself over the last few days concerning these topics.
Was This Post Helpful? 0
  • +
  • -

#5 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1854
  • View blog
  • Posts: 6,676
  • Joined: 19-February 09

Re: Help with binary file reading

Posted 23 April 2016 - 05:25 PM

Hi, your hop count is 57054 which is DEDE in hexadecimal (used calculator). DE in decimal is 222, part of source address.

So it looks like packing and hidden bytes.

Suggest search for C++ pack.
Was This Post Helpful? 0
  • +
  • -

#6 csnook   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 23-April 16

Re: Help with binary file reading

Posted 23 April 2016 - 05:36 PM

View Post#define, on 23 April 2016 - 05:25 PM, said:

Hi, your hop count is 57054 which is DEDE in hexadecimal (used calculator). DE in decimal is 222, part of source address.

So it looks like packing and hidden bytes.

Suggest search for C++ pack.

Is it possible to give me a quick rundown on packing. I have seen the #pragma pack used, would that factor in here?
Was This Post Helpful? 0
  • +
  • -

#7 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1854
  • View blog
  • Posts: 6,676
  • Joined: 19-February 09

Re: Help with binary file reading

Posted 23 April 2016 - 06:17 PM

I notice that one field is commented out, if left in it may allow data alignment (although not future-proof) on a word boundary.

The compiler can align data in stored memory. Word (etc.) alignment forces integer or larger items to align (start) on multiples of the alignment size. Extra bytes are used in records to allow alignment.

The data was read to your structure as a string of bytes, so some bytes were read to the extra bytes.

#pragma pack()
Was This Post Helpful? 0
  • +
  • -

#8 csnook   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 23-April 16

Re: Help with binary file reading

Posted 23 April 2016 - 07:30 PM

So I take it there is something wrong with my read then, I'm still at a loss
Was This Post Helpful? 0
  • +
  • -

#9 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1854
  • View blog
  • Posts: 6,676
  • Joined: 19-February 09

Re: Help with binary file reading

Posted 23 April 2016 - 07:38 PM

Why is this line commented out?

17	//unsigned char services;


Was This Post Helpful? 0
  • +
  • -

#10 csnook   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 23-April 16

Re: Help with binary file reading

Posted 23 April 2016 - 07:39 PM

View Post#define, on 23 April 2016 - 07:38 PM, said:

Why is this line commented out?

17	//unsigned char services;



Its not needed, it was never part of the original struct we had to use and runs without it
Was This Post Helpful? 0
  • +
  • -

#11 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1854
  • View blog
  • Posts: 6,676
  • Joined: 19-February 09

Re: Help with binary file reading

Posted 23 April 2016 - 07:56 PM

I think your options are to either try the pragma pack directive or read into each field variable from the file.
Was This Post Helpful? 0
  • +
  • -

#12 csnook   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 23-April 16

Re: Help with binary file reading

Posted 23 April 2016 - 08:09 PM

This is my updated code and produces this:
Attached Image
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;
#pragma pack(push, 1)
// header struct definition
struct s_header {
	unsigned char verCode;
	unsigned char serviceType;
	unsigned short totLength;
	unsigned char reserved;
	unsigned char flags;
	unsigned char codes;
	unsigned short hopCount;
	unsigned long srcAddr1;
	unsigned long srcAddr2;
	unsigned long destAddr;
} fileheader;// you can make a global header variable RIGHT HERE if you want to
#pragma pack(pop)
// string tables
char * codes[] = {
	"OK",
	"Possible data corruption"
	"Reserved"
};

char * services[] = {
	"Normal",
	"Low Priority",
	"High Priority",
	"Medium Priority",
	"So-so Priority",
	"Needed Yesterday",
	"Nevermind"
	"Reserved"
};

char * flags[] = {
	"Grumpy",
	"Sneezy",
	"Bashful",
	"Doc",
	"Dopey",
	"Tipsy",
	"Surly",
	"Snow White"
};

int main(int argc, char * argv[]) {
	char buffer[1024];
	char ch, code;
	int x;

	ifstream myfile("PACKETS.txt", ios::in | ios::binary);
	if (!myfile) {
		cout << "Cannot open file.\n";
		return 1;	
	}

	myfile.read((char *)& fileheader, sizeof s_header);

	fileheader.verCode = fileheader.verCode >> 4;
    cout << "Version: " << setw(8) << (int)fileheader.verCode << endl;
	fileheader.verCode = fileheader.verCode >> 3;
	cout << "Code: " << setw(11) << (int) fileheader.verCode << " - ";
	code = fileheader.codes;
	for (int i = 0; i < sizeof(fileheader.verCode); i++) {
		cout << codes[i] << endl;
	}

	cout << "Total Length: " << setw(4) << fileheader.totLength << endl;
	cout << "Service Type: " << setw(3) << (int)fileheader.serviceType << " - ";
	cout << services[fileheader.serviceType] << endl;
	
	x = fileheader.flags;
	for (int i = 0; i < 8; i++) 
	{
		cout << "    " << flags[i] << ":\t" << (x & 1) << endl;
		x = x >> 1;
	}
	
	cout << "Hop Count: " << fileheader.hopCount << endl;	
	cout << "Source Address: " << fileheader.srcAddr1 << "." << fileheader.srcAddr2 << endl;
	cout << "Dest Address: " << fileheader.destAddr << endl;	
	cout << "Data: ";


	myfile.close();
	cout << endl;
	system("pause");
}



Was This Post Helpful? 0
  • +
  • -

#13 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1854
  • View blog
  • Posts: 6,676
  • Joined: 19-February 09

Re: Help with binary file reading

Posted 23 April 2016 - 08:19 PM

Can you use a calculator to figure out what the hop count value is in hex? There might be a convertor online.
Was This Post Helpful? 0
  • +
  • -

#14 csnook   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 23-April 16

Re: Help with binary file reading

Posted 23 April 2016 - 08:21 PM

DE00
Was This Post Helpful? 1
  • +
  • -

#15 GetSet   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 411
  • Joined: 08-February 11

Re: Help with binary file reading

Posted 23 April 2016 - 08:25 PM

View Postkaa, on 23 April 2016 - 04:54 PM, said:

I don't have time to get into the details but I suggest that you check sizeof(fileheader) and compare that to the sum of the sizes of the individual fields in the struct. I suspect that there may be some padding in there since the struct seems to contain an odd number of bytes.

It would probably be helpful if you would post a sample input file for people to test with.


Up one. A sample input file is necessary for any serious debugging.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2