C实现复制文件

本文介绍了一个简单的文件复制程序实现,使用C语言通过命令行参数指定源文件和目标文件,利用系统调用完成文件读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码

//mycp.c

  1 #include <unistd.h>
  2 #include <sys/stat.h>
  3 #include <sys/types.h>
  4 #include <fcntl.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 
  8 #define SIZE 8192
  9 
 10 int main(int argc, char *argv[]){
 11 
 12     char buf[SIZE];
 13     int fd_src, fd_dest;
 14     int len;
 15 
 16     if (argc < 3){
 17 
 18         printf("./mycp ssrc dest\n");
 19         exit(1);
 20     }
 21 
 22     fd_src = open(argv[1],O_RDONLY);
 23     fd_dest = open(argv[2],O_CREAT | O_WRONLY | O_TRUNC ,0666);
 24 
 25     while((len = read(fd_src, buf, sizeof(buf))) > 0){
 26 
 27         write(fd_dest, buf, len);
 28     }
 29 
 30     close(fd_src);
 31     close(fd_dest);
 32 
 33 }
~                                                                                           
~                                 

执行

#gcc mycp.c -o mycp
#./mycp mycp.c mycp_cp.c
#cat mycp_cp.c 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值