The start pointer of array will change if you call read() function, because you want to read the file in a stream style instead of always starting over.
No, you don't have to take care of this.
The only thing is when you call read4() which reads 4 bytes into your buffer you might read more than you need, so you want to store those bytes in the structure, and next time you call read will start from those stored bytes, then read more from the file.
Think that you have 4 chars "a, b, c, d" in the file, and you want to call your function twice like this:
- read(buf, 1); // should return 'a'
- read(buf, 3); // should return 'b, c, d'
All the 4 chars will be consumed in the first call. So the tricky part of this question is how can you preserve the remaining 'b, c, d' to the second call.
@laonawuli What if we call read4 and use buf-- to get back to a?
I think the difference is:
Call once: Assume you are always going to read from the start of the file/bufer.
Call multiple times: Start reading from where you left off. This means that you have to store the last place (ptr) where you stopped and store the read but uncopied bytes to the buffer.
I think code wise it should be same for both the cases except that the pointer from where to start reading the internal read4 buffer, the internal read4 buffer itself and the number of bytes to be read from that buffer, need to be stored in the 2nd case.
In plain C, I think if these fields are defined as static (int the 2nd case), then we will achieve the purpose.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.