编程复制( cp)

gcc cp.c -o mycp.c

./mycp demo.c demo1.c  //将demo.c 复制到 demo1.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc,char **argv)
{
    //1.打开src.c
    //2.读src.c到buf
    //3.打开/创建des.c
    //4.将buf写入到des.c
    //5.close两个文件
    int fdsrc;
    int fddes;
//    char readbuf[1024]={0};
        char *readbuf=NULL;  

 //判断
    if(argc !=3){
        printf("pararm error!");
        exit(-1);    
    }
    fdsrc=open(argv[1],O_RDWR);

    int size=lseek(fdsrc,0,SEEK_SET);

   readbuf=(char *)malloc(sizeof(char)*size);


    int n_read=read(fdsrc,readbuf,size);
    fddes=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
    int n_write=write(fddes,readbuf,strlen(readbuf));

    close(fdsrc);
    close(fddes);


    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/weixin_62529596/article/details/128138410
cp