如何使用 read() 部分读取输入直到换行?

How to partially read input until a new line using read()?

我正在尝试部分读取输入,直到输入新行“\n”。但是我怎样才能用 read() 函数做到这一点呢?现在我已经进行了部分读取,直到注销终端界面并停止输入(在 linux 终端上按 ctrl+d),但不知道如何在换行时停止输入。这是我的代码:

int fd = 0;
const size_t read_size = 100;
size_t size = read_size;
char *buff = malloc(size+1);
size_t offset = 0;
size_t res = 0;

while((res = read(fd, buff + offset, read_size)) > 0)
{
    offset += res;
    buff[offset] = '[=10=]';
    if (offset + read_size > size)
    {
        size *= 2;
        buff = realloc(buff, size+1);
    }
}
return buff;

I'm trying to partially read input until a new line is inputed "\n". But how can I do that with the read() function?

使用 read 实现此目的的唯一方法是一次读取一个字符。读完 '\n' 字符后,停止。

how do I check what is being read?

while (read(fd, buf + offset, 1) == 1) {
  if (buf[offset] == '\n') break;
  offset += 1;
}