简单的实现一下文件的复制操作,直接贴源码了,中间也有一些注释,至于更多的详细的命令参数,推荐看下这篇博客,讲的很详细:传送门
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define maxn 1005
int main(int agrc, char *agrv[])
{
if(agrc < 3){ // 如果传入的参数不够3个直接退出
printf("./copy error!\n");
exit(0);
}
int len;
char buf[maxn];
int fd_file = open(agrv[1], O_RDONLY); // open一个只能读的文件 在agrv[1]中
// open一个只能写的文件 如果不存在就新创建一个 如果存在O_TRUNC可以将其内容大小设置为0
// 因为有O_CREAT参数 所以最后还需要设置文件权限
int fd_aim = open(agrv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
// while循环不断从fd_file中读取数据
while((len = read(fd_file, buf, sizeof(buf))) > 0){
write(fd_aim, buf, len); // 将读到的数据写入fd_aim,注意长度为len
}
return 0;
}