版权声明:[email protected] https://blog.csdn.net/zhaoxuyang1997/article/details/81638082
/*Linux C :Copy oldFilePath to newFilePath by ZhaoXuyang at 2018-05-24 02:23*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main(){
char oldFilePath[255];
char newFilePath[255];
printf("Copy oldFile to newFile:\n");
printf("Input old file path:");
scanf("%s",oldFilePath);
printf("Input new file path:");
scanf("%s",newFilePath);
int fd1=open(oldFilePath,O_RDONLY);
if(fd1<0){
printf("Can't open %s\n",oldFilePath);
exit(1);
}
int fd2=open(newFilePath,O_CREAT|O_TRUNC|O_RDWR,0644);
if(fd2<0){
printf("Can't open %s\n",newFilePath);
exit(1);
}
int words;
const int len=255;
char buf[len];
while((words=read(fd1,buf,len))>0){
int n=write(fd2,buf,words);
if(n<0){
printf("Write fail!\n");
exit(1);
}
}
close(fd2);
close(fd1);
return 0;
}