> For the complete documentation index, see [llms.txt](https://netjimmy.gitbook.io/code-interview-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://netjimmy.gitbook.io/code-interview-note/simulation/read4.md).

# Read4

## Read N Characters Given Read4

The API: int read4(char \*buf) reads 4 characters at a time from a file.

By using the read4 API, implement the function int read(char \*buf, int n) that reads n characters from the file.

* create char\[] 接收API傳進來的資料

```python
def read(self, buf, n):
    idx = 0
    buf4 = [''] * 4
    while idx < n:
        num = read4(buf4)
        if num == 0:
            break
        i = 0
        #                讀進來沒那麼多
        while idx < n and i < num:
            buf[idx] = buf4[i]
            i+=1
            idx+=1        
    return idx
```

## Read N Characters Given Read4 II

The read function may be called multiple times.

Example 1:

Given buf = "abc" read("abc", 1) // returns "a"&#x20;

read("abc", 2); // returns "bc" read("abc", 1); // returns ""&#x20;

```python
def __init__(self):
    self.buf4 = [0] * 4
    self.buf4_size = 0
    self.buf4_idx = 0
        
        
def read(self, buf, n):
    idx = 0
    while idx < n:
        if self.buf4_idx == 0:
            self.buf4_size = read4(self.buf4)
            if self.buf4_size == 0: break
        
        #                        讀進來沒那麼多
        while idx < n and self.buf4_idx < self.buf4_size:
            buf[idx] = self.buf4[self.buf4_idx]
            idx+=1
            self.buf4_idx+=1
        
        #  需要重讀 read4   
        if self.buf4_idx >= self.buf4_size:
            self.buf4_idx = 0

    return idx
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://netjimmy.gitbook.io/code-interview-note/simulation/read4.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
