假如你有 1.txt 2.txt 运行程序就会把1.txt 的内容复制到2.txt中去
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
//打开文件
int fd1 = open("1.txt", O_RDWR);
int fd2 = open("2.txt", O_RDWR);
//if(fd == -1)
if(-1 == fd1 || -1 == fd2)
{
printf("open 1.txt error\n");
}
printf("open success\n");
//读写文件
char buf[4096] ={
while(1)
{
int r = read(fd1,buf,4096);
if(r <=0)
{
break;
}
r = write(fd2,buf,r);
}
//关闭文件
close(fd1);
close(fd2);
return 0;
}