I use this routine to do a binary read from file to memory, which worked fine until now… on iOS at least, in the simulator(…don’t have the paid developer program(yet 😉 ))
The numbers e.g. fileSize, bytesRead are OK, but it gives gibberish at the end…
I can’t have overwritten memory, since I do the output right away…
Then I thought it could be an alignment boundary issue e.g. fileSize % 4 = det. gibberish.
But that would be strange behavior, the function gets a size and a count, the lib should calc a multiple byte read on the background, so that shouldn’t cause the problem…
Here’s the code I use:
uint8_t *readFileToMemory(FILE *fp)
{
fseek(fp, 0, SEEK_END);
long fileSize = ftell(fp);
rewind(fp);
//
printf("fileSize %lu bytes\n",fileSize);
//
uint8_t *fileData = NULL;
//
fileData = (uint8_t *)requestMemory(fileData, (MEM_TYPE_MEMSIZE)fileSize, BF_MEM_ZERO_NO, "readFileToMemory()");
fread(fileData, 1, (size_t)fileSize, fp);
//
long sizeRead = fread(fileData, 1, (size_t)fileSize, fp);
printf("sizeRead %lu bytes\n",sizeRead);
//
fclose(fp);
//
printf("+\n+\nfileData:\n%s+\n+\n",fileData);
//
return fileData;
}
The reason why I post this question is “WHY the gibberish?” on iOS-sim, I do have a simple workaround btw…
Niels
I am here now, so I might as well provide an answer for closure 😉
I forgot to put an ‘\0’ at the end of the binary file before passing it along as (c/glsl NULL-terminated)string… Oops 🙂
I thank borrrden for his remarks on the Apple libs binary-read, which I still need to look into… (time time time)
Niels