Read N characters Given Read4 ii-call multiple times

Source: Internet
Author: User

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.

read4by 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.