One sure problem is this code:
raw_data = (char*) malloc (size);
size = fread (raw_data, 1, size, target);
raw_data[size] = '\0';
You cannot access raw_data[size]
, because it is beyond the allocated size. Indexed access in C/C++ is zero based. As a result, the last element of raw_data
that can be accessed with your existing code is raw_data[size-1]
. To be able to set the byte which is at offset size
to zero you need to change your malloc
to:
raw_data = (char*) malloc (size+1);
Since this is a C++ application, you may want to use streams and new/delete instead of FILE pointers and malloc/free.