0

I don't think I've ever been so frustrated with a pset so far. I sort of understand what I have to do, but I can't just write proper code that'll make it happen. I don't think I've learnt pointers well enough for this. My idea for the pseudo code is:

While (!feof)

 if Fread from file into buffer > 0

   realloc *content with the size of buffer

   memcpy buffer into content

   *length++

am I in the right track? is there something I'm missing? thanks for the help edit: formatting

2

Definitely the right track.

This While (!feof) is fraught. Remember the warnings from Week 4 > Shorts > File I/O starting around 7:30. So somehow "wrap" your while around the fread.

Instead of realloc *content with the size of buffer consider realloc *content to it's current size plus the size of buffer

Instead of memcpy buffer into content consider memcpy buffer to the end of content

Instead of *lenght++ consider *lenght+= some number of bytes, (like what it just read?)

Frustration is the enemy. This pset is quite challenging, to be sure. Trust what you know, research/review what you don't and use the tools (debug50, gdb, valgrind).

1

Hint: look at the top of the server.c distribution code for a symbolic constant define which suggests a good size (BYTES) for your buffer. Read in the file one byte at a time, incrementing a counter (bytes) for each byte read.

Now ask yourself these questions:

  • How will you know that your buffer is full and you need to realloc()? Can you devise a simple test for this, perhaps using the modulo operator?
  • How will you determine the new size of the buffer? Can you express this as an equation using your bytes counter and the BYTES symbolic constant?

You must log in to answer this question.

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