Linux 系统IO函数 复制文件内容

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

int main()
{
//打开存在的文件
int fd=open("test.txt",O_RDONLY);
if(fd==-1)
{
perror("open");
exit(1);
}
//创建一个新的文件,写操作
int fd1=open("newfile",O_WRONLY|O_CREAT,0664);
if(fd1==-1)
{
perror("open1");
exit(1);
}
//read file
char buf[2048]={0};
int count=read(fd,buf,sizeof(buf));
if(count==-1)
{
perror("read");
exit(1);
}
while(count!=0)
{
//将读出数据写入另一个文件
int ret=write(fd1,buf,count);
printf("write bytes %d ",ret);

count=read(fd,buf,sizeof(buf));

}
close(fd);
close(fd1);
}

原文地址:https://www.cnblogs.com/sclu/p/11241024.html