(一)发行版:Ubuntu16.04.7
(二)记录:
(1)不传参,指定拷贝文件,指定复制到文件
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc,char *argv[])
{
int copya_fd;
int copyb_fd;
ssize_t read_byte;
ssize_t write_byte;char buff[36]={0};//打开要复制的文件
copya_fd = open("copya.c",O_RDWR);
if(copya_fd == -1)printf("fail to open copya.c\n");
elseprintf("open copya.c successfully:%d\n",copya_fd);
//打开将要复制到的文件
copyb_fd = open("copyb.c",O_RDWR|O_CREAT,0666);
if(copyb_fd == -1)printf("fail to open copyb.c\n");
elseprintf("open copyb.c successfully:%d\n",copyb_fd);
//读取文件内容到buff中
read_byte=read(copya_fd,buff,36);
write_byte=write(copyb_fd,buff,36);
printf("read %d bytes from copya.c\n",read_byte);
printf("write %d bytes to copyb.c\n",write_byte);
//关闭文件
close(copya_fd);
close(copyb_fd);return 0;
}
makefile
copy:main.cgcc main.c -o copyclean:rm -rf copy copyb.c
(2)参入文件夹拷贝
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc,char *argv[])
{
int copya_fd;
int copyb_fd;
ssize_t read_byte;
ssize_t write_byte;char buff[36]={0};if(argc != 3)
{
printf("error number of file\n");
return 0;
}
//打开要复制的文件
copya_fd = open(argv[1],O_RDWR);
if(copya_fd == -1)printf("fail to open copyed\n");
elseprintf("open copyed successfully:%d\n",copya_fd);
//打开将要复制到的文件
copyb_fd = open(argv[2],O_RDWR|O_CREAT,0666);
if(copyb_fd == -1)printf("fail to open copy\n");
elseprintf("open copy successfully:%d\n",copyb_fd);
//读取文件内容到buff中
read_byte=read(copya_fd,buff,36);
write_byte=write(copyb_fd,buff,36);
printf("read %d bytes from copyed\n",read_byte);
printf("write %d bytes to copy\n",write_byte);
//关闭文件
close(copya_fd);
close(copyb_fd);return 0;
}
makefile
copy:main.cgcc main.c -o copychuancan:chuancan.cgcc chuancan.c -o chuancanclean:rm -rf copy copyb.c chuancan
第二种方法中,因为./exe为第一个参数,所以为argv[0],所以我们传入的参数实际为argv[1]和argv[2]。 所以我们的argv[1]为copya.c,argv[2]为copye.c
./chuancan copya.c copye.c
(三)命令: