0

After many hours of grinding with this pset I finally seem to have both lookup() and parse() functions working correctly.

I tried a few methods for load() and finally seem to have it close to working, when i go to /cat.html my console reports:

GET /cat.html HTTP/1.1

HTTP/1.1 200 OK

GET /favicon.ico HTTP/1.1

HTTP/1.1 200 OK

but notice it didn't call cat.jpg as it should have, and my browser window remains empty.

If i go to cat.jpg directly the console reports:

GET /cat.jpg HTTP/1.1

HTTP/1.1 200 OK

GET /favicon.ico HTTP/1.1

HTTP/1.1 200 OK

but i only get an empty frame on my browser window.

Here's a link to my load() function implementation:

https://gist.github.com/anonymous/89981b46e7d3014102b5578b4f3ab975

Could you please help me figure out why are files not loading when they appear they are. Thank you.

1

According to the fread man page:

SYNOPSIS
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

DESCRIPTION
The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

RETURN VALUE
On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

You are therefore telling the fread function to read 1 chunk of data of size BYTES but the function returns the number of chunks it read which can be only 0 or 1 in this case.

5
  • Thank you. I was under the impression the return value would be the same as for the read() function used by the staff on their implementation of request().
    – dLopez
    Jul 30 '16 at 8:19
  • You also shouldn't use !feof(file) as the condition in your while loop, because if an error occurred, you could no longer read the file and you would be stuck in the loop. See Week 4 - Shorts - File I/O.
    – mimo31
    Jul 30 '16 at 8:27
  • Would the following solve this problem?: for ( int c = fgetc(flile); c != EOF; c = fgetc(file) )
    – dLopez
    Jul 30 '16 at 11:55
  • @dLopez That actually reads a byte from the file. Therefore, that byte would not be read the fread function. I would instead check if bytes (the number of bytes read into the buffer) is not 0.
    – mimo31
    Jul 30 '16 at 21:08
  • Right, i tried replacing the fread function altogether by using that format of for loop and then each passby assigning c to content[length] but that didn't work
    – dLopez
    Jul 30 '16 at 21:53

You must log in to answer this question.

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