I've tried for about five days to finish the load function but now that I have two compact versions which don't work I don't have any idea what i am doing wrong.
Code for the first load function:
bool load(FILE* file, BYTE** content, size_t* length){
if(file == NULL){
return false;
}
int b;
*content = NULL;
BYTE* buffer = "";
size_t fileSize = 0;
for(b = fgetc(file); b != EOF; b = fgetc(file)){
buffer = addByte(buffer, b);
fileSize++;
*content = realloc(*content, fileSize);
}
strcpy(*content, buffer);
*length = fileSize;
return true;
}
... and the addByte function...
BYTE* addByte(BYTE* str, BYTE b){
BYTE* bP = malloc(sizeof( b + 1) * sizeof(bP) );
bP[0] = b;
bP[1] = '\0';
BYTE* res = malloc((strlen(str) + strlen(bP)) * sizeof(res));
strcpy(res, str);
strcat(res, bP);
return res;
free(res);
free(bP);
}
The other load function:
bool load(FILE* file, BYTE** content, size_t* length){
*content = NULL;
size_t fileSize = 0;
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
fseek(file, -fileSize, SEEK_CUR);
rewind(file);
*content = malloc(fileSize * sizeof(BYTE) * sizeof(BYTE**) * sizeof(*content));
fread(*content, sizeof(BYTE), fileSize, file);
length = &fileSize;
return true;
}
I don't see any error in the functions. in the first function the cat.html file loads but the image cat.jpg won't. The alt text of the tag is displayed instead. cat.jpg doesn't load at all and favicon.ico doesn't load also(by that I mean the seperate files of the directory). With the second load function cat.jpg in cat.html won't show and the alt text appears again and when I reload the page a random sized wall of random characters appears. favicon.ico appears to be running but in hello.php a segmentation fault occurrs.
Thanks in advance :)
UPDATE
I've tried to make the load function smaller without using my own addByte()
function. Instead I used the BYTE* buffer variable as an array and added each b from the for-loop of my first load function for(int b = fgetc(file); b != EOF; b = fgetc(file))
etc. on the i'th place of buffer. Here is my current code:
bool load(FILE* file, BYTE** content, size_t* length)
{
int i = 0;
size_t fileSize = 0;
*content = NULL;
BYTE *buffer = NULL;
for(int b = fgetc(file); b != EOF; b = fgetc(file)){
fileSize++;
buffer = realloc(buffer, fileSize);
buffer[i] = b;
i++;
}
*length = fileSize;
*content = buffer;
return true;
}
The only thing I found out in the last days was that feof(file)
's integer value is 0 and the integer value of EOF
is -1. Due to the fact that no functions like stcpy()
or memcpy()
are used I think that there is something basic I don't understand. Maybe it is simple pointer arithmetic? Maybe I don't understand realloc()
? Maybe it is something with the null-terminator \0
or I don't understand a concept fully yet. I'm starting to freak out honestly.
check50 failures
And when I click on cat.html this happens...