Posted on

Description

Submission

/**
 * The read4 API is defined in the parent class Reader4.
 *     int read4(char *buf);
 */

class Solution {
public:
    /**
     * @param buf Destination buffer
     * @param n   Number of characters to read
     * @return    The number of actual characters read
     */
    int read(char *buf, int n) {
        int count = 0;
        int total = 0;
        char b[4];
        for( ; total < n && (count = read4(b)) ; total += count) {
            for(int i = total; i < min(n, count + total); ++i) {
                buf[i] = b[i-total];
            }
        }
        total = min(total, n);
        buf[total] = 0;
        return total;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *