#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFSIZE 1024
int main(int argc, char **argv)
{
if (argc < 3)
{
fprintf(stderr, "usage %s\n", argv[0]);
}
int fd_s;
int fd_d;
fd_s = open(argv[1], O_RDONLY, 0600);
if (fd_s < 0)
{
perror("open() ");
exit(1);
}
fd_d = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd_d < 0)
{
close(fd_s);
perror("open() ");
exit(1);
}
char buf[BUFSIZE];
while (1)
{
int rnum, wnum;
int off = 0;
rnum = read(fd_s, buf, BUFSIZE);
if (rnum < 0)
{
perror("read()");
exit(1);
}
if (rnum == 0)
{
break;
}
while (rnum != 0)
{
wnum = write(fd_d, buf + off, rnum);
if (wnum < 0)
{
perror("write()");
exit(1);
}
off += wnum;
rnum -= wnum;
}
}
close(fd_s);
close(fd_d);
return 0;
}
03-20
1933
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-27
276
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-01