1、使用read、write实现两个文件的拷贝
include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define R O_RDONLY
#define W O_WRONLY|O_CREAT|O_TRUNC
#define A O_WRONLY|O_CREAT|O_ACCMODE
#define Rp O_RDWR
#define Wp O_RDWR|O_CREAT|O_TRUNC
#define Ap O_RDWR|O_CREAT|O_TRUNC
int main(int argc, char const *argv[])
{
int fd1 = 0;
int fd2 = 0;
int ret = 0;
int cnt = 0;
char str[5] = {0};
if((fd1 = open("./file1.txt",R)) == -1)
{//文件打开失败
perror("open file");
return -1;
}
if((fd2 = open("./file2.txt",W,0666)) == -1)
{//文件打开失败
perror("open file");
return -1;
}
do
{
ret = read(fd1,str,sizeof(str));
cnt+=ret;
write(fd2,str,ret);
}while(ret);
printf("文件大小: %d\n",cnt);
close(fd1);
close(fd2);
return 0;
}