Linux写一个CP指令(其实就是文件读写操作)

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
int main(int argc,char **argv)//这里的指针存放的是文件地址
{
    
    
        int fdNow;
        int fdGoal;
        char *readBuf=NULL;

        if(argc!=3)
        {
    
    
                printf("Error!\n");
                exit(-1);
        }
        fdNow=open(argv[1],O_RDWR);//第一个位置代表需要拷贝的内容
        int n=lseek(fdNow,0,SEEK_END);//求取文件大小,为开辟readBuf做准备
        lseek(fdNow,0,SEEK_SET);//光标回为
        readBuf=(char *)malloc(sizeof(char)*n+2);
        int n_read=read(fdNow,readBuf,n);

        fdGoal=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);//第二个位置代表需要拷贝到什么地方,若那个地方原来有数据,则清除然后存放拷贝的内容。
        write(fdGoal,readBuf,strlen(readBuf));

        close(fdNow);
        close(fdGoal);

        return 0;
}

在这里插入图片描述
在这里插入图片描述

cp3.c里面的内容与cp2.c里面的一样

猜你喜欢

转载自blog.csdn.net/qq_43482790/article/details/115085832
今日推荐