利用fread将文件内容输出在终端上
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE* fp= fopen("./fwrie.txt","r");
if(NULL==fp){
printf("打开文件失败\n");
return -1;
}
int a[5]={65,97,66,98,67};
char str[11]="helloworde";
//int res=fwrite(a,sizeof(int),5,fp);
char c;
while(fread(&c,1,sizeof(char),fp)!=0){
printf("%c",c);
}
// fread(&c,1,);
// fwrite(str,1,11,fp);
return 0;
}
利用read和write实现文件拷贝
#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("./01_open.c",O_RDONLY);
int ptw=open("./read.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;
}