用read4實現readn...
至調用一次,感覺怎么搞都可以。。。估計這個題有II就是調用多次了。。。
感覺多次勇哥buffer存下多讀的那部分就好了。。。
// Forward declaration of the read4 API. int read4(char *buf); class Solution { public: /** * @param buf Destination buffer * @param n Maximum number of characters to read * @return The number of characters read */ int read(char *buf, int n) { char buffer[5]; int cnt = 0; while (cnt < n) { int sz = read4(buffer); memcpy(buf + cnt, buffer, sz); cnt += sz; if (sz < 4) break; } if (cnt > n) { buf[n] = '\0'; cnt = n; } return cnt; } };