Linux系统函数_read和write实现文件拷贝功能 代码,练习所写不足之处请在下方评论指出
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#define N 1024
int main(int argc, char const* argv[])
{
int fd, fd_out;
int n;
char buf[N];
fd = open(argv[1], O_RDONLY);
if(fd < 0)
{
perror("open 1 error");
exit(1);
}
fd_out = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);
if(fd_out < 0)
{
perror("open 2 error");
exit(1);
}
while((n = read(fd, buf, N)))
{
if(n < 0)
{
perror("read error");
exit(1);
}
write(fd_out, buf, n);
}
close(fd);
close(fd_out);
return 0;
}