Hello World,

I am currently working on pset6 and I think I have completed most of the word needed, but the browser keeps on not showing cat.jpg and cat.html. I think there is something wrond in my load function, which I quote underneath.

bool load(FILE* file, BYTE** content, size_t* length)
// allocates memory to hold bytes
char* bytes = malloc(sizeof(char));
if(bytes == NULL)
    return false;

// int to hold the nuber of bytes read
int num = 0;

// reads bytes character by character
for(char c = fgetc(file); c != EOF; c = fgetc(file))
    // stores character while updating number of bytes
    bytes[num] = c;

    // reallocates memory for next char
    bytes = realloc(bytes, sizeof(char) * (num + 1));

// stores information into parameters
*content = bytes;
*length = num;

return true;

Here are some screenshots of what is the output of my requests for the cat file. http://prntscr.com/bbtazt

Thanks a lot.


You are reading bytes in using fgetc, I made this same mistake and it turns out that although a char is 1 byte and you are wanting to read the image file 1 byte at a time, fgetc will specifically only work with chars; so it will show your html because they are using the correct datatype.

I don't want to give away too much so my advice is to take a look at your solution to pset4 recover and how you read those bytes in; you will find the answer there!

  • You are right, I re-implemented my load function using a while loop and fread and it worked. But I still cannot see the reason, why fgetc doesn't work properly there. – Lysandros Nikolaou Jun 4 '16 at 15:01
  • I'm glad it worked for you, I had the same problem and couldn't understand it for ages, but C is so strict with its datatypes and fgetc is for chars; even though what you're wanting is 1 byte at a time (therefore the size of a char), it still won't read it in. – Claire Jun 4 '16 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .