编程实现CP指令:
步骤:
1.打开第一个目标文件
2.读取里面的数据
3.打开第二个目标文件
4.将读取的数据写入
5.关闭文件
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv)
{
int fdSrc;
int fdDes;
if(argc != 3){
printf(“fail\n”);
exit(-1);
}
fdSrc = open(argv[1],O_RDWR);
int size = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
char *readBuf = (char *)malloc(sizeof(char)*size + 8);
int n_read=read(fdSrc,readBuf,size);
fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0666); //O_TRUNC 如果原来文件中有数据全部清除掉
int n_write = write(fdDes,readBuf,strlen(readBuf));
close(fdSrc);
close(fdDes);
return 0;
}