#Error while allocating and loading data from binary file

24 messages · Page 1 of 1 (latest)

worldly citrus
#

hi, I'm trying to allocate some space with malloc to load a file as a character array, but I'm getting some weird data, the expected value of the first byte of the array is 0x49 ( "I" in the magic word in header) but I'm getting 0x0, 0x10 or 0x4C, this is what I wrote to get to allocate this data:

char* ID3Section = (char*)malloc(headerSize);
    
fread(ID3Section, headerSize, headerSize, song->fptr);
printf("data stored at: 0x%x\n", ID3Section);
printf("0x%x\n", ID3Section[0]);
return ID3Section;```
trim pollenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dawn mortar
#

such trick would only work if mp3 header alignment would match your target architecture data alignment

#

also endianess

#

ah, I'm blind

#

you are using char

#

start with using unsigned char

#

and headerSize, headerSize makes no sense

#

as it asks fread to read headerSizer * headerSize bytes

worldly citrus
#

oh wait

dawn mortar
#

so it should be headerSize, 1

worldly citrus
#

oh wait yeah I'm telling it to load a huge ammout of data

#

I'm still getting random values though..

dawn mortar
#

maybe you should start by checking if fread succeeded, because maybe you have errors earlier?

#

it should return number of elements you asked for, so 1 in case of headerSize, 1

worldly citrus
#

it does seem to be working, I load the first 10 bytes as an array before loading the whole header to check if ID3 is in the file and the size

dawn mortar
#

that moves file pointer

worldly citrus
#

and it does open the file and identify the header

dawn mortar
#

so the next read starts from 11th byte

worldly citrus
#

oh wait fr?

dawn mortar
#

you may use rewind() to go back

worldly citrus
#

that fixed it lol

#

now I just gotta figure out why strstr is not working..

#

!solved