0

I watched the walkthrough for load, but I am struggling to understand what the problem set is asking for. What is load's objective, and what is the best way to approach load?

I also don't know whether to use a for loop or a while loop. The walkthrough mentioned that I had to read until the end of the file, but I don't know how a for loop or a while loop could be used for that. Is one better for this problem set?

Thanks in advance.

1 Answer 1

2

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.

4
  • Hello! What is the error exactly? can you post your code to pastebin and the put the link here? And please read this first just to see if it's enough: cs50.stackexchange.com/a/2301/13557 Commented Dec 3, 2016 at 1:40
  • The error is the following: incompatible pointer types assigning to 'BYTE *' (aka 'char *') from 'char **'; remove &, and is throwing the error at *content = &buffer; Here is the link: pastebin.com/f1TCNBya Commented Dec 3, 2016 at 14:30
  • 1
    You were trying to assing a pointer to char to a pointer to pointer. *content points at BYTE/char type, buffer also points at BYTE/char type. So the types match and it works. But &buffer is the address of a pointer type (the address of buffer, which is a pointer), thus the type would be pointer to pointer and the types won't match. Commented Dec 3, 2016 at 15:06
  • Thank you so much! Commented Dec 4, 2016 at 1:36

You must log in to answer this question.

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