您的位置:首页 > 运维架构 > Linux

Linux文件系统编程 系统调用 文件复制

2017-08-06 23:08 239 查看
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define SIZE 1024

int main()
{
// 打开要读的文件
int fd1 = open("1.ppt", O_RDONLY);
if (fd1 == -1)
{
perror ("open fd1");
return -1;
}

int fd2 = open("2.ppt", O_WRONLY|O_CREAT, 0777);
if (fd2 == -1)
{
perror ("open fd2");
return -1;
}

int ret = 0;
char buf[SIZE] = {0};
while (ret = read (fd1, buf, SIZE))
{
if (ret == -1)
{
perror("read");
break;
}

write (fd2, buf, ret);
}
printf ("文件复制完成\n");
close (fd1);
close (fd2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: