Practice/Google/Leetcode 157. Read N Characters Given Read4
CodingMust
You are working with a file reading system that provides a low-level API called read4. This API reads up to 4 characters at a time from a file into a temporary buffer and returns the actual number of characters read.
Your task is to implement a higher-level read function that uses read4 to read exactly n characters (or fewer if the end of file is reached) into a destination buffer.
The read4 API signature:
buf4 - a temporary buffer array that can hold 4 charactersbuf4. Returns the count of characters actually read. Returns 0 when the end of file is reached.read(buf, n) function that reads n characters into the destination buffer bufread4 API to access the filen charactersn is not a multiple of 4buf has enough space to hold n charactersread4 API and cannot directly access the fileExample 1:
` File content: "abcdefghij" n = 5
Process:
Output: 5 `
Example 2:
` File content: "abc" n = 10
Process:
Output: 3 Explanation: Only 3 characters exist, so only 3 can be read `
Example 3:
` File content: "abcdefgh" n = 8
Process:
Output: 8 `
Example 4:
` File content: "" n = 1
Process:
Output: 0 `