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 are only 3 characters left in the file.
read4
by using the API, implement the function that int read(char *buf, int n)
reads n characters from the file.
Note:
The read
function may be called multiple times.
1 /*The read4 API is defined in the parent class Reader4.2 int read4 (char[] buf);*/3 4 Public classSolutionextendsReader4 {5 /**6 * @parambuf Destination Buffer7 * @paramN Maximum number of characters to read8 * @returnThe number of characters read9 */Ten Private Char[] tmp =New Char[4]; One Private intTmppoint;//indicate which position we should start reading A Private intTmpcontain;//indicate how many char in the TMP buff - Public intReadChar[] buf,intN) { - intCur = 0; the while(Cur <N) { - if(Tmppoint = = 0){ -Tmpcontain =READ4 (TMP); - } + if(Tmpcontain = = 0){ - returncur; + } A while(cur < n && Tmppoint <Tmpcontain) { atBuf[cur + +] = Tmp[tmppoint + +]; - } -Tmppoint = tmppoint%Tmpcontain; - } - returncur; - } in}
Read N characters Given Read4 ii-call multiple times