#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int pt=open("./1.read.c",O_RDONLY);
int ptw=open("./write.text",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(pt<0){
perror("open");
return -1;
}
ssize_t res;
char buf[2];
while(1){
bzero(buf,sizeof(buf));
res=read(pt,buf,sizeof(buf));
if(res==0){
printf("读取完成\n");
break;
}
else if(res<0){
perror("read");
break;
}
write(ptw,buf,sizeof(buf));
}
return 0;
}
【IO】使用read和write实现文件拷贝
最新推荐文章于 2024-07-15 10:12:43 发布