tags:

views:

22

answers:

0

Hello. I am trying to read random binary data (just to gain understanding and fluency with c++), and display the bit patterns in hex. The code below, as I understand it, should read 5 elements, each 1 byte, and store each element in the char array 'testing'. My code is:

int main() {
  char paus[2];
  int n=0;
  FILE *fid;
  int elem_size = 1;
  int num_items = 5;
  int testing_size = elem_size*num_items;
  printf("testing_size: %d\n",testing_size);
  char *testing = new char[testing_size];
  int size_of;

  fid = fopen("C:\\Users\\JMP\\Desktop\\test.dat","rb");
  if(!fid)
     printf("open failed\n");
  else {
     fseek(fid,6104,SEEK_SET);
     fread(testing,elem_size,num_items,fid);
     fclose(fid);
  }
  sizeo_of = sizeof(testing);
  printf("sizeof(testing): %d\n",size_of);
  for (n=0; n<testing_size; n++)
      printf("testing: %x\n",testing[n]);

  fgets(paus,2,stdin);
  return 0;
}

And the output is:

testing_size: 5
sizeof(testing): 4
testing: 0
testing: 7a
testing: 5b
testing: 4c
testing: ffffffa0

I know sizeof(testing) won't be the size of the array. But why is it 4 bytes? Is that the size of the pointer? And I know the order of the bits in each of the 5 bytes I'm trying to read. Each of the elements of testing has the correct hex value for the bits as I know them to be, except the last. Why is this element apparently 4 bytes long? The a0 is the correct bit ordering of the last byte, but why are there all the f's before it? I don't know if the answer is platform dependent, but I'm using a windows 32bit machine (and visual c++ 2008). Thanks for any enlightenment.