实现Linux下的cp命令

cp命令的作用:读取源文件写到目标文件

具体实现思路:

1.打开源文件,先判断argc==3,argv[0]为可执行程序的名字,argv[1]为源文件,argv[2]为目标文件

2.当源文件存在的时候用O_EXCL进行一次提示

3.当源文件与目标文件都正确打开后,循环读取源文件写入目标文件

4.完成实现后可以用vimdiff  文件A  文件B 可以比较两个文件相同与否

具体实现代码:

  1 #include<stdio.h>                                                           
  2 #include<stdlib.h>
  3 #include<unistd.h>
  4 #include<fcntl.h>
  5 #include<errno.h>
  6 #include<string.h>
  7 int main(int argc,char *argv[])
  8 {
  9     if(argc!=3)
 10     {
 11         fprintf(stderr,"usage:%s src dst\n",argv[0]);
 12         exit(1);
 13     }
 14     int fd_src=open(argv[1],O_RDONLY);
 15     if(fd_src==-1)
 16     {
 17         perror("open");
 18         exit(1);
 19     }
 20     int fd_dest=open(argv[2],O_WRONLY|O_CREAT|O_EXCL,0644);
 21     if(fd_dest==-1&&errno==EEXIST)
 22     {
 23         printf("recover it?");
24         char choose;
 25         scanf("%c",&choose);
 26         if(choose=='y'||choose=='Y')
 27         {
 28             fd_dest=open(argv[2],O_WRONLY);
 29         }
 30         else
 31         {
 32             exit(1);
 33         }
 34     }
 35     while(1)
 36     {
 37         char buf[1024]={};
 38         memset(buf,0x00,sizeof(buf));
 39         int r=read(fd_src,buf,1024);
 40         if(r==-1)
 41         {                                                                   
 42             perror("read");
 43             exit(1);
 44         }
 45         if(r==0)
 46         {
 47             break;
 48         }
 49         write(fd_dest,buf,r);
 50     }
 51     close(fd_src);
 52     close(fd_dest);
 53 }                       

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux下,可以使用C语言实现cp命令。具体步骤如下: 1. 打开源文件和目标文件,使用open函数,其中源文件以只读方式打开,目标文件以写方式打开。 2. 使用read函数从源文件中读取数据,使用write函数将数据写入目标文件中。 3. 如果源文件中还有数据未读取完毕,则继续读取并写入目标文件中。 4. 关闭源文件和目标文件,使用close函数。 5. 如果出现错误,则使用perror函数输出错误信息。 下面是一个简单的实现示例: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #define BUFFER_SIZE 1024 int main(int argc, char *argv[]) { int source_fd, target_fd; ssize_t read_size, write_size; char buffer[BUFFER_SIZE]; if (argc != 3) { fprintf(stderr, "Usage: %s <source_file> <target_file>\n", argv[]); exit(EXIT_FAILURE); } source_fd = open(argv[1], O_RDONLY); if (source_fd == -1) { perror("open source file"); exit(EXIT_FAILURE); } target_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); if (target_fd == -1) { perror("open target file"); exit(EXIT_FAILURE); } while ((read_size = read(source_fd, buffer, BUFFER_SIZE)) > ) { write_size = write(target_fd, buffer, read_size); if (write_size != read_size) { perror("write target file"); exit(EXIT_FAILURE); } } if (read_size == -1) { perror("read source file"); exit(EXIT_FAILURE); } if (close(source_fd) == -1) { perror("close source file"); exit(EXIT_FAILURE); } if (close(target_fd) == -1) { perror("close target file"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值