Q1. Should I use fgetc() or fread()
A1.: You can make both work! Choose the one you're more comfortable with.
Q2. I don't know whether to use a for loop or a while loop. Is one better for this pset?
A2.: In C you can translate a for
loop to a while
loop and vice-versa. So, again, choose the one you like best.
Here's some pseudocode for you to begin with:
1 - create a buffer and malloc() a block of memory to it
2 - keep reading bytes from file to buffer, until the end of the file, but:
2.1 - increase length by the numbers of bytes read.
2.2 - If the next read will exceed the current size of the buffer, realloc() more memory to it.
3 - After the EOF, make the content argument point at that block of memory that buffer is pointing at.
Basically, you should use what you learned on pset5 when you implemented load there.
PS: You could do this without a buffer, malloc
'ing memory to the content
pointer directly, but to beginners I think it is easier to use a buffer.