Reputation: 4004
I have a file that was created by an application on the same PC as my code. This has the following at the start:
$ od -c save.data
0000000 \0 \0 \0 006 \0 \0 \0 \0 D \0 V \0 E \0 1
.
.
Using the following code in C to read the file in:
FILE *encfile;
char inba[8192]; // input file is < 500 bytes so this is plenty big enough
memset(inba, 0, sizeof(inba));
encfile = fopen(argv[1], "rb");
int br = fread(inba, 1, 8192, encfile);
printf ("Bytes read: %d\n", br);
The above prints Bytes read: 409
correctly.
The issue is that the inba
variable is 'empty', because I think the first byte read is 0
, which gets treated as end of string. There are a number of 0
value bytes in the file.
How can I get C to see this as a 409 byte array?
The data will get passed to other functions that expect this as a char *
variable.
Is this an endianess issue?
Upvotes: 0
Views: 167
Reputation: 75062
Binary data is generally not strings. Keep track the size by yourself.
This code will dump the contents of the array in hexadecimal:
// br and inba are the variable in OP's code
for (int i = 0; i < br; i++) {
printf("%02X", (unsigned char)inba[i]);
if (i + 1 >= br || i % 16 == 15) putchar('\n'); else putchar(' ');
}
Upvotes: 3