linux实现CP指令

简介: linux实现CP指令

实现CP指令只需要五步

1.打开Src文件(源文件)

2.读Src到Buf

3.打开Des文件(目标文件)

4.写Buf到Des

5.关闭Src和Des文件

代码如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
 
int main(int argc, char **argv)
{
    int fdSrc;
    int fdDes;
 
    char *readBuf=NULL;
 
    if(argc != 3){
        printf("pararm error\n");
        exit(-1);
    }
    
    fdSrc = open(argv[1],O_RDWR);
    int size = lseek(fdSrc,0,SEEK_END);
    lseek(fdSrc,0,SEEK_SET);
 
    readBuf=(char *)malloc(sizeof(char)*size + 8);
 
    int n_read = read(fdSrc, readBuf, size);
    
    //O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。
    fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
       
    //strlen()计算字符串长度
    int n_write = write(fdDes,readBuf,strlen(readBuf));
 
    close(fdSrc);
    close(fdDes);
 
    return 0;
}
相关文章
|
8天前
|
Linux
Linux:文件读取指令
Linux:文件读取指令
16 2
|
21小时前
|
人工智能 Java Shell
【Linux】环境变量及相关指令
【Linux】环境变量及相关指令
|
22小时前
|
存储 监控 Unix
【Linux 系统】Linux 下基本指令 -- 详解
【Linux 系统】Linux 下基本指令 -- 详解
|
1天前
|
Linux 开发工具
linux sudo指令提权
linux sudo指令提权
|
1天前
|
算法 Unix Linux
Linux基本指令总结(二)
Linux基本指令总结(二)
|
1天前
|
Linux C语言 Windows
linux基本指令总结--文件和目录(一)
linux基本指令总结--文件和目录(一)
|
2天前
|
消息中间件 运维 Linux
运维最全Linux 命令大全之scp命令_linux scp 指令(1),2024年最新从消息中间件看分布式系统的多种套路
运维最全Linux 命令大全之scp命令_linux scp 指令(1),2024年最新从消息中间件看分布式系统的多种套路
|
8天前
|
缓存 安全 Linux
Linux入门基本指令(2)
Linux入门基本指令(2)
16 0
|
8天前
|
Linux Windows
Linux入门基本指令(1)-2
Linux入门基本指令(1)
17 1
|
8天前
|
Linux 数据安全/隐私保护 Windows
Linux入门基本指令(1)-1
Linux入门基本指令(1)
18 1