What is the difference between call once and call multiple times?

  • 48

    Just wondering what is the difference between call once and call multiple times.

  • 5

    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.

  • 0

    Thanks. so we need a pointer to record the index of the array?

  • 50

    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.

  • 0

    very clear explaination

  • 100

    Think that you have 4 chars "a, b, c, d" in the file, and you want to call your function twice like this:

    1. read(buf, 1); // should return 'a'
    2. 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.

  • 0

    @laonawuli What if we call read4 and use buf-- to get back to a?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.