C语言允许按指定格式读写文件。函数fscanf()用于按指定格式从文件读数据。其函数原型为:
int fscanf (FILE *fp, const char *format ,...);
其中,第一个参数为文件指针,第2个参数为格式控制参数,第3个参数为输出参数列表,后两个参数和返回值与函数scanf()相同。
函数fprintf()用于按指定格式像文件写数据。其函数原型为:
int fprintf(FILE *fp,const char *format ,...);
其中,第1个参数为文件指针,第2个参数为格式控制参数,第3个参数为输出参数列表,后两个参数和返回值与函数printf()相同。
用函数fscanf()和fprintf()进行文件的格式化读写,读写方便,容易理解,但输入时要将ASCII字符转换称二进制数,输出时要将二进制数转换为ASCII字符,耗时较多。
例题1:编程计算每个学生的四门课的平均分,将学生个各科成绩及平均分输出到文件score.txt中。
#include <stdio.h>
#include <stdlib.h>
#define N 30
typedef struct date
{int year;int month;int day;
}DATE;
typedef struct student
{long studentid;char studentname[10];char studentsex;DATE birthday;int score[4];float aver;
}STUDENT;
void inputscore(STUDENT stu[],int n,int m);
void averscore(STUDENT stu[],int n,int m);
void writefile(STUDENT stu[],int n,int m);
int main(void)
{STUDENT stu[N];int n;printf("How many student?");scanf("%d",&n);inputscore(stu,n,4);averscore(stu,n,4);writefile(stu,n,4);return 0;
}void inputscore(STUDENT stu[],int n,int m)
{int i,j;for(i=0;i<n;i++){printf("Input record %d:\n",i+1);scanf("%ld",&stu[i].studentid);scanf("%s",stu[i].studentname);scanf(" %c",&stu[i].studentsex);scanf("%d",&stu[i].birthday.year);scanf("%d",&stu[i].birthday.month);scanf("%d",&stu[i].birthday.day);for(j=0;j<m;j++){scanf("%d",&stu[i].score[j]);}}
}void averscore(STUDENT stu[],int n,int m)
{int i,j,sum;for(i=0;i<n;i++){sum=0;for(j=0;j<m;j++){sum = sum+stu[i].score[j];}stu[i].aver = (float)sum/m;}
}void writefile(STUDENT stu[],int n,int m)
{FILE *fp;int i,j;if((fp = fopen("score.txt","w"))==NULL){printf("Failure to open score.txt!\n");exit(0);}fprintf(fp,"%d\t%d\n",n,m);for(i=0;i<n;i++){fprintf(fp,"%10ld%8s%3c%6d%02d/%02d",stu[i].studentid,stu[i].studentname,stu[i].studentsex,stu[i].birthday.year,stu[i].birthday.month,stu[i].birthday.day);for(j=0;j<m;j++){fprintf(fp,"%4d",stu[i].score[j]);}fprintf(fp,"%6.lf\n",stu[i].aver); }fclose(fp);}
例题2:编程从文件score1.txt中读取出每个学生的学号、姓名、性别、出生日期、各科成绩及平均分,并输出到屏幕上。
#include <stdio.h>
#include <stdlib.h>
#define N 30
typedef struct date
{int year;int month;int day;
}DATE;
typedef struct student
{long studentid;char studentname[10];char studentsex;DATE birthday;int score[4];float aver;
}STUDENT;
void readfromfile(STUDENT stu[],int *n,int *m);
void printscore(STUDENT stu[],int n,int m);
int main(void)
{STUDENT stu[N];int n,m=4;readfromfile(stu,&n,&m);printscore(stu,n,m);return 0;
}void readfromfile(STUDENT stu[],int *m,int *n)
{FILE *fp;int i,j;if((fp= fopen("score.txt","r"))==NULL){printf("Failure to open score.txt!\n");exit(0);}fscanf(fp,"%d\t%d",n,m);for(i=0;i<*n;i++){fscanf(fp,"%10ld",&stu[i].studentid);fscanf(fp,"%8s",stu[i].studentname);fscanf(fp,"%c",&stu[i].studentsex);fscanf(fp,"%6d/%2d/%2d",&stu[i].birthday.year,&stu[i].birthday.month,&stu[i].birthday.day);for(j=0;j<*m;j++){fscanf(fp,"%4d",&stu[i].score[j]);}fscanf(fp,"%f",&stu[i].aver);}fclose(fp);
}void printscore(STUDENT stu[],int n,int m)
{int i,j;for(i=0;j<n;i++){printf("%10ld%8s%3c%6d/%02d/%02d",stu[i].studentid,stu[i].studentname,stu[i].studentsex,stu[i].birthday.year,stu[i].birthday.month,stu[i].birthday.day);for(j=0;j<m;j++){printf("%4d",stu[i].score[j]);}printf("%6.lf\n",stu[i].aver);}
}