我不明白我执行 cp 命令有什么问题

I don't understand what's wrong in my implementation of cp command

我正在尝试使用 C 在 UNIX 上模拟 cp。这是我的代码。

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char const *argv[])
{
  int src, dest;
  char buff[256];
  int bits_read;

  src = open(argv[1], O_RDONLY);
  dest = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0644);

  if (dest < 0)
    perror("Er");

  while ((bits_read = read(src, buff, sizeof(buff))) > 0)
    if (bits_read != write(dest, buff, sizeof(buff)))
      perror("Er");

  close(src);
  close(dest);

  return 0;
}

我得到以下输出:

Er: Undefined error: 0

我可以看到新文件末尾有一些重复的行。

最后一行不是 sizeof(buf) long。
使用

if (bits_read != write(dest, buff, bits_read))