#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
int fd1, fd2;
int count, count2;
char buffer[1024] = {0};
char *ptr;
if (argc != 3)
{
printf("Usage: %s file1 file2\n", argv[0]);
return 1;
}
if (-1 == (fd1 = open(argv[1], O_RDONLY)))
{
perror("open file1\n");
close(fd1);
return 2;
}
if (-1 == (fd2 = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)))
{
perror("open file2\n");
close(fd2);
return 3;
}
while (count = read(fd1, buffer, 1024))
{
if (-1 == count)
{
perror("read");
return 4;
}
ptr = buffer;
int count2 = write(fd2, ptr, count);
if (-1 == count2)
{
perror("write\n");
return 5;
}
memset(buffer, 0, 1024);
}
close(fd1);
close(fd2);
return 0;
}
Linux系统调用实现文件复制
最新推荐文章于 2022-02-26 09:23:01 发布