I've been struggling with this one for a while now. I'm trying to read a file with hexadecimal information in it, lets say the contents of the file looks like this.
00 00 e0 3a 12 16 00 ff fe 98 c4 cc ce 14 0e 0a aa cf
I'm looking for a method of reading each 'byte' of information (whilst ignoring whitespace) in C++.
Since the code I'm writing is part of a class I've created a small program to demonstrate what I'm currently doing.
int main(void)
{
int i = 0;
unsigned char byte;
unsigned char Memory[50];
ifstream f_in;
f_in.open("file_with_hex_stuff.txt");
f_in >> skipws; // Skip Whitespace
while(f_in >> hex >> byte)
{
Memory[i++] = byte;
}
return 0;
}
(apologies if the code above doesn't compile, it's just so you can get a feel for what I'm wanting. I expected the array to be like this:
Memory[0] => 0x00;
Memory[1] => 0x00;
Memory[2] => 0xe0;
Memory[3] => 0x3a;
Memory[4] => 0x12;
etc....
But instead it loads each of the numbers into it's own position in the array like this:
Memory[0] => 0x00; // 2 LOCATIONS USED FOR 0x00
Memory[1] => 0x00; //
Memory[2] => 0x00; // 2 LOCATIONS USED FOR 0x00
Memory[3] => 0x00; //
Memory[4] => 0x0e; // 2 LOCATIONS USED FOR 0xe0
Memory[5] => 0x00; //
Memory[6] => 0x03; // 2 LOCATIONS USED FOR 0x3a
Memory[7] => 0x0a; //
etc...
Applogies if this post makes no sense. Any feedback appreciated.
Thanks.