The API:
int read4(char *buf) reads 4 characters at a time from a file.
The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
By using the
read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
Note:
The
The
read function will only be called once for each test case.
每次只读4个,因此不能用buf直接给read4. 因为如果读五个,直接传buf的时候,读第五个地方的时候会覆盖第一个位置
public int read(char[] buf, int n) {
int count = 0;
while(count < n){
char[] tmp = new char[4];
int c= read4(tmp);
for(int j = 0; j < c && count < n; j++){
buf[count++] = tmp[j];
}
if(c < 4) break;
}
return count;
}
没有评论:
发表评论