#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd1 = -1;
int fd2 = -1;
int size = 0;
char *buf;
if(argc < 3)
{
printf("用法 copy [源文件/路径] [目标文件/路径]\n");
return -3;
}
if( (fd1 = open(argv[1], O_RDONLY)) < 0 )
{
printf("没有%s\n",argv[1]);
return -1;
}
if( (fd2 = open(argv[2], O_CREAT, 0600)) < 0 )
{
printf("无法创建%s,或者目标已存在.\n",argv[2]);
return -2;
}
else
{
close(fd2);
fd2 = open( argv[2], O_WRONLY);
}
size = lseek(fd1, 0, SEEK_END);
buf = (char *) malloc(size);
lseek(fd1, 0, SEEK_SET);
read(fd1, buf, size);
write(fd2, buf, size);
close(fd1);
close(fd2);
return 0;
}
实现linux cp命令( 系统调用版 )
C文件复制程序
最新推荐文章于 2023-04-10 14:02:16 发布
本文介绍了一个简单的C语言程序,用于复制文件。该程序接受两个参数:源文件路径和目标文件路径,并通过使用标准C库函数如open、read、write等实现了文件内容的逐字节复制。如果目标文件已存在,则会覆盖原有内容。

566

被折叠的 条评论
为什么被折叠?



