Binary I/O File into Byte Array

Sep 25, 2012 at 9:42pm
I want to be able to read a file (in Hex format) and place it into a byte[] array. If someone could code this for me and include comments where necessary. Thanks.
Last edited on Sep 25, 2012 at 9:45pm
Sep 25, 2012 at 10:01pm
How about you attempt your homework and we will help if u get stuck. If you are already stuck you should have paid attention in class.
Sep 26, 2012 at 10:23am
This isn't for school or education. I'm learning C++ for my own purpose. This would help me a-lot of C++. It would explain something I've always been confused about.

The code is kind of in 'testing state'. I want oData to become a byte array. I was thinking of passing a pointer to a byte array to this function so I could write to that specific byte array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DWORD GetHandshakeCommand(char * filename)
{
	ifstream inFile; 
	size_t size = 0;
	inFile.open("Data\\SecAPI", ios::in|ios::binary|ios::ate);
	
	if(inFile.is_open()) {

		char* oData = 0;

		size = inFile.tellg();
		inFile.seekg(0, ios::beg);

		oData = new char[size+1];
		inFile.read(oData, size);
		oData[size] = '\0';
	}

	return 0;
}
Last edited on Sep 26, 2012 at 12:01pm
Sep 26, 2012 at 3:56pm
1
2
size = inFile.tellg();
inFile.seekg(0, ios::beg);

This won't tell you the size of the file. You're asking where the get pointer is, hint: it hasn't moved since you opened the file, and then you setting the get pointer back to the beginning of the stream. You're obviously using the WinAPI in other parts of your code so why not just stick with it? "GetFileSize()" is a pretty straight forward function: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364955(v=vs.85).aspx . Let us know if you need help with it. Regarding the name of this function, are you working with a network stream? Because if that's the case then we need to rethink this whole thing. Also, what's the thing you've been confused about? I can understand if you want to figure it out for yourself but I'd like to at least offer you an answer.

EDIT: D'oh! I missed the "ios::ate" on Line 5. I would still encourage you to use "GetFileSize()" but you're fine.
Last edited on Sep 26, 2012 at 5:54pm
Sep 26, 2012 at 4:53pm
Fixed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
DWORD GetHandshakeCommand(char * filename)
{
	ifstream inFile; 
	size_t size = 0;
	inFile.open("Data\\SecAPI", ios::in|ios::binary|ios::ate);
	
	if(inFile.is_open()) {

		char* oData = 0;

		inFile.seekg(0, ios::end);
		size = inFile.tellg();
		inFile.seekg(0, ios::beg);

		oData = new char[size+1];
		inFile.read(oData, size);
		oData[size] = '\0';
	}

	return 0;
}


The problem just is getting a char array (oData) into a byte array(Hex Format). I'm not sure on how that could be achieved.
Sep 26, 2012 at 5:13pm
A byte array means an unsigned char array. If you want a hexacedimal string representation of that array use sprintf() C function.
Sep 26, 2012 at 6:06pm
Last edited on Sep 26, 2012 at 6:55pm
Topic archived. No new replies allowed.