How copy binary data to string of type char*?

Hi, I try to solve this problem very long. I just cannot solve it what I do wrong. I have stuct FILES and its member program is type char*. I created "instance" called files. Then in a loop I go through files[n] ... files[count] and I access files[count].program where I want to copy data read from binary file (length is 11000 bytes). So in the loop I open the file as binary, I read it as binary.

Now my problem is:
1) do I do the allocation of memory correctly? I tried skip cast and I tried to use cast as this (char*) both failed.
2) I need to check that the lengh is 11000 bytes and I try to use sizeOf. When I would use str * char;sizeOf(str); so it will show size of pointer which is 4 bytes; wo I need to use something like str * char; sizeOf(*str), right?

In the code bellow I got this:
1
2
3
4
5
6
7
8
printf("expected:%ld\n",files[count].size);
printf("sizeof(*(files[count].program)):%d\n",sizeof(*(files[count].program)));
/** ALLOCATE MEMORY **/
files[count].program = (char*) malloc(files[count].size);
fsize = fread (files[count].program,1,files[count].size,f2);
if (fsize != files[count].size) {fputs (" Error: reading of kernel failed.",stderr); exit (3);}
printf("sizeof(*(files[count].program)):%d\n",sizeof(*(files[count].program)));
printf("fsize:%ld ",fsize);


expected:11000
sizeof(*(files[cound],program)):1
sizeof(*(files[cound],program)):1
fsize:11000
...
The fsize tells me only how many bytes was read not how many bytes was written to char string. When I change the command sizeOf(files[count].program) shows 4 bytes as expected.

Pls help how to copy the binary data to the buffer. Do I do it right? How to check the length/size of the binary string files[cound],program?
Last edited on
h4ever wrote:
The fsize tells me only how many bytes was read not how many bytes was written to char string.
Shouldn't the two always be the same? (assuming you have opened the file in binary mode)
But how can I check that the data are correct? I am doubtful. My program crashes when I try to import it into OpenCL to create kernel (program). So I suspect that the string is incorrectly loaded. I need to check the length of the binary string to be sure that it is OK.


In Code::Blocks when I check the pointer with a watch:
Before the memory allocation the pointer is 0x0 . After malloc the pointer is 0x4c66e8 + sequences representing the chars.

After fread tt does not look that the data are valid. I got 0x4c66e8 "\177ELF\001\001\001" for the member program when the data should be loaded. It should be like: 0x4c66e8 "\177ELF\001\001\001\001\001\001\001\001\001\001\001..."

Last edited on
Are you sure the debugger is not just showing the first few bytes because showing them all would take up the whole screen? Maybe there is some option (a plus sign or a right click menu) that you can use to view the whole array content. I don't use Code::Blocks so I don't know.
I am sure it shows correct. After malloc there is pretty huge amout of sequences:

0x4c66e8 "\rđ­\272\rđ­\272\rđ­\272\rđ­\272\rđ­\272\rđ­\272\rđ­\272\rđ­\272\rđ­\272\rđ­\272\..."

I can view it no problem. But after fread I see only these: 0x4c66e8 "\177ELF\001\001\001" . This is not just wrong interpretation of \001\ because you can see 3 times \001. If I am correct this is 7 bytes. So I should be able to get at least value 7 as size of the string. So why I get 4 (in the case that I use sizeof(files[count].program) or 1 when I use sizeof(*(files[count].program))?
It shows "ELF" as letters instead of the numeric values "\069\076\070" so I suspect it handles the array content as a text string. The end of a text string is usually marked with a null character (zero byte) so Code::Blocks probably stop printing the characters after the first zero byte for that reason.

files[count].program is a pointer so sizeof(files[count].program) will give you the size of the pointer. There is no way you can get the size of an array created with malloc. You know the size of the array is files[count].size and you know that fread has written fsize bytes to it. There is nothing more you need to know.
Could I at least check the value of e.g. files[count].program[8] or files[count].program[12]?

printf("fsize:%s ",files[count].program[8]);
gives me warning:
comparison between pointer and integer [enabled by default]

The best would be if I could print the numerical value of the nth element of the char* string. The \000 probably wont print anything.
Last edited on
Try %d instead of %s. Note that the eighth byte is at index 7 (not 8).

EDIT: Wikipedia says that this byte is often set to 0 regardless of the target platform.
http://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header
Last edited on
Ok, so I added this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
printf("test:%d\n",files[count].program[14]); // 0
printf("test:%d\n",files[count].program[15]); // 0
printf("test:%d\n",files[count].program[30]); // 0
printf("test:%d\n",files[count].program[31]); // 0
printf("test:%d\n",files[count].program[32]); // -21
printf("test:%d\n",files[count].program[33]); // 41
printf("test:%d\n",files[count].program[34]); // 0
printf("test:%d\n",files[count].program[35]); // 0
printf("test:%d\n",files[count].program[36]); // 0
printf("test:%d\n",files[count].program[37]); // 0
printf("test:%d\n",files[count].program[38]); // 0
printf("test:%d\n",files[count].program[39]); // 0
printf("test:%d\n",files[count].program[40]); // 52 (=HEX 34)
printf("test:%d\n",files[count].program[41]); // 0
printf("test:%d\n",files[count].program[42]); // 0
printf("test:%d\n",files[count].program[43]); // 0
printf("test:%d\n",files[count].program[44]); // 0
printf("test:%d\n",files[count].program[45]); // 0
printf("test:%d\n",files[count].program[46]); // 40 (=HEX 28)
printf("test:%d\n",files[count].program[47]); // 0
printf("test:%d\n",files[count].program[48]); // 7
printf("test:%d\n",files[count].program[50]); // 1 

And the printed values are OK. Big relief. Thanks.
Topic archived. No new replies allowed.