自行实现Linux环境下的cp命令

一. 创建a.txt文件

  1. 创建a.txt空文件 vi a.txt
  2. 输入i,进入编辑模式,写入内容
  3. 按下Esc键,输入:wq

二. 编写mycp函数

  1. 创建mycp.c文件 vi mycp.c
  2. 输入i,进入编辑模式,写入代码
#incLude <stdio.h>
#include <string.h>
#incLude <stdLib.h>
int main(int argc,char *argv[]){
   FILE * fin;
   FILE * fout;
   int ch;
   char *f_read = argv[1];
   char *f_write = argv[2];

   fin = fopen(f_read,"rb");
   fout = fopen(f_write,"wb");
   ch = getc(fin);
   while(ch != EOF)
   {
 	putc(ch,fout);
  	ch = getc(fin);
    }
   fclose(fin);
   fcLose(fout);
   return 0;
}
  1. 按下Esc键,输入:wq

三. 编译mycp.c文件

gcc mycp.c -o mycp

四. 将生成的可执行文件移动到系统变量目录

mv mycp /usr/bin

五.测试mycp命令

mycp a.txt b.txt

六.查看b.txt文件内容

image

posted @ 2021-11-29 11:42  博客用户66  阅读(113)  评论(0)    收藏  举报