#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
int my_read(FILE*fp)//作业一
{
if(NULL==fp)
{
perror("err:");
return -1;
}
char str;
while(sizeof(str)==fread(&str,1,sizeof(str),fp))
{
printf("%c",str);
}
}
int main(int argc, const char *argv[])
{
/* FILE*fp=fopen("./one.c","r");
my_read(fp);
if(fclose(fp)==EOF)
{
perror("eer1:");
return-1;
}*/
int fp2=open("hw.txt",O_RDWR|O_CREAT|O_TRUNC,0777);
if(fp2<0)
{
perror("err2:");
return -1;
}
char strr[20]="hello world";
if(write(fp2,strr,sizeof(strr))<0)
{
perror("err_wirt");
}
close(fp2);
int fp3=open("hw.txt",O_RDWR);
int cpy=open("my_cpy.txt",O_RDWR|O_CREAT|O_TRUNC,0777);
char c;
ssize_t num;
while(1)
{
num=read(fp3,&c,1);
write(cpy,&c,sizeof(c));
printf("%c",c);
if(num==0)
{
printf("输出完成\n");
break;
}
if(num<0)
{
perror("ree_read");
break;
}
}
if(close(fp3)<0)
{
perror("close");
return -1;
}
close(cpy);
return 0;
}
文件io cpy
最新推荐文章于 2024-07-20 09:51:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)