5、到文本文件中,再用字符方式从此文本
文件中逐个读入并显示在终端屏幕上。#includevoidfun(char*s,inta,doublef){/**********found**********/FILE*fp;charch;fp=fopen("file1.txt","w");fprintf(fp,"%s%d%f",s,a,f);fclose(fp);fp=fopen("file1.txt","r");printf("Theresult:");ch=fgetc
6、(fp);/**********found**********/while(!feof(fp)){/**********found**********/putchar(ch);ch=fgetc(fp);}putchar('');fclose(fp);}main(){chara[10]="Hello!";intb=12345;doublec=98.76;fun(a,b,c);}3.程序通过定义学生结构体变量,存储了学生的学号
、姓名和3门课的成绩。所有学生数据均以二进制方式
输出到文件中。函数
7、fun的功能是重写形参filename所指文
件中最后一个学生的数据,即用新的学生数据覆盖该学
生原来的数据,其它学生的数据不变。#include#defineN5typedefstructstudent{longsno;charname[10];floatscore[3];}STU;voidfun(char*filename,STUn){FILE*fp;/**********found**********/fp=fopen(filename,"rb+");/*********
8、*found**********/fseek(fp,-1L*sizeof(STU),SEEK_END);/**********found**********/fwrite(&n,sizeof(STU),1,fp);fclose(fp);}main(){STUt[N]={{10001,"MaChao",91,92,77},
{10002,"CaoKai",75,60,88},{10003,"LiSi",85,70,78},
{10004,"FangFang",90,82,87},{10005,"Z
9、hangSan",95,80,88}};STUn={10006,"ZhaoSi",55,70,68},ss[N];inti,j;FILE*fp;fp=fopen("student.dat","wb");fwrite(t,sizeof(STU),N,fp);fclose(fp);fp=fopen("student.dat","rb");fread(ss,sizeof(STU),N,fp);fclose(fp);printf("Theoriginaldata:");for(j=0;j10、;j++){printf("No:%ldName:%-8s
Scores:",ss[j].sno,ss[j].name);for(i=0;i<3;i++)printf("%6.2f",ss
[j].score[i]);printf("");}fun("student.dat",n);printf("Thedataaftermodifing:");fp=fopen("student.dat","rb");fread(ss,sizeof(STU),N,fp);fclose(fp)