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


  • 48
    H

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


  • 5
    M

    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
    H

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


  • 50
    M

    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
    D

    very clear explaination


  • 100
    L

    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
    Y

    @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.