Part1: 验证性实验
验证性实验2
如果事先不知道学生人数,
改变后的代码如下:
// 从文本文件file1.dat中读取数据,找出最高分和最低分学生信息,并输出在屏幕上 #include <stdio.h> #include <stdlib.h> #define N 10 // 定义一个结构体类型STU typedef struct student { int num; char name[20]; int score; }STU; int main() { STU st, stmax, stmin; int i; FILE *fp; // 以只读文本方式打开文件file1.dat fp = fopen("file1.dat", "r"); if( !fp ) { // 如果打开失败,则输出错误提示信息,然后退出程序 printf("fail to open file1.dat\n"); exit(0); } stmax.score = 0; // 先假定最高分是0,后面如发现比当前最高分还高的分数,就更新最高分 stmin.score = 100; // 先假定最低分是100分,后面如发现比当前最低分更低的分数,就更新最低分 while( !feof(fp) ) { fscanf(fp, "%d %s %d", &st.num, st.name, &st.score); // 从fp指定的文件中格式化读取一个学生信息 if(st.score > stmax.score) stmax = st; else if(st.score < stmin.score) stmin = st; } fclose(fp); printf("最高分学生信息: %5d%15s%5d\n", stmax.num, stmax.name, stmax.score); printf("最低分学生信息: %5d%15s%5d\n", stmin.num, stmin.name, stmin.score); return 0; }
运行结果为:
二进制文件与文本文件的区别:
验证性实验3程序源码及运行结果:
fout = fopen("file3.dat", "w");
验证性实验4程序源码及运行结果:
fout = fopen("file4.dat", "wb");
结合实验三和实验四可知:
以写的方式打开或创建二进制文件为:“wb”;
以写的方式打开或创建文本文件为“w”。
文本文件储存量大,速度慢,但直观,便于对字符操作;
而二进制文件存储量小、速度快、便于存放中间结果,但不直观。
写一个简单的程序,尝试从二进制文件?le4.dat中读出数据,并在屏幕上显示,以此查看文件?le4.dat的内 容。给出这个程序源码和运行截图:
#include <stdio.h> #include <stdlib.h> #define N 10 typedef struct student { int num; char name[20]; int score; }STU; int main(){ int i; STU st[N]; FILE *fout; fout = fopen("file4.dat", "rb"); if( !fout ) { // 如果打开失败,则输出错误提示信息,然后退出程序 printf("fail to open file4.dat\n"); exit(0); } // 从fout指向的数据文件file4.dat中读取数据到结构体数组st for(i=0; i<N; i++) { fread(&st[i],sizeof(STU),1,fout); printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score); } fclose(fout); // 关闭fout指向的文件file4.dat return 0; }
Part2: 编程练习
代码如下:
#include <stdio.h> #include <string.h> const int N = 10; // 定义结构体类型struct student,并定义其别名为STU typedef struct student { long int id; char name[20]; float objective; /*客观题得分*/ float subjective; /*操作题得分*/ float sum; char level[10]; }STU; // 函数声明 void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { STU stu[N]; printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n"); process(stu, N); printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n"); output(stu, N); return 0; } // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 void input(STU s[], int n) { int i; FILE *fp; fp=fopen("examinee.txt","r") ; for(i=0;i<N;i++) { fscanf(fp,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); } fclose(fp); } // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 // 不仅输出到屏幕上,还写到文本文件result.txt中 void output(STU s[], int n) { int i; FILE *fp; fp=fopen("result.txt","w"); for(i=0;i<N;i++){ fprintf(fp,"%ld %8s %10lf %10lf %10lf %5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); printf("%ld %8s %10lf %10lf %10lf %5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); } fclose(fp); } // 对考生信息进行处理:计算总分,排序,确定等级 void process(STU s[], int n) { int i,j; STU temp; for(i=0;i<N;i++){ s[i].sum=s[i].subjective+s[i].objective; } for(i=0;i<N-1;i++){ for(j=0;j<N-i-1;i++){ if(s[j+1].sum>s[j].sum){ temp=s[j]; s[j]=s[j+1]; s[j+1]=temp; } } } for(i=0;i<N;i++){ if((i+1)<=0.1*N) strcpy(s[i].level,"优秀"); if((i+1)>0.1*N&&(i+1)<=0.5*N) strcpy(s[i].level,"合格"); if((i+1)>0.5*N) strcpy(s[i].level,"不合格"); } }
运行结果:
Part3: 拓展综合应用
代码如下:
#include <stdio.h> #include <stdlib.h> #define N 80 typedef struct student { int number; int num; char name[20]; char c[20]; }STU; int main(){ int i; STU st[N]; int a[5]; FILE *fout,*fin; fout = fopen("list.txt", "r"); if( !fout ) { // 如果打开失败,则输出错误提示信息,然后退出程序 printf("fail to open list.txt\n"); exit(0); } // 从fout指向的数据文件list.txt中读取数据到结构体数组st for(i=0; i<N; i++) { fscanf(fout, "%d %d %s %s", &st[i].number, &st[i].num,st[i].name, st[i].c); } for(i=0; i<5; i++) { a[i]= (rand() % N)-1; } // 以写方式打开/创建文件lucky.txt fin = fopen("lucky.txt", "w"); if( !fin ) { // 如果打开失败,则输出错误提示信息,然后退出程序 printf("fail to open lucky.txt\n"); exit(0); } // 将排序后的数组st中数据输出到屏幕 for(i=0; i<5; i++) { printf("%5d %10d %10s %15s\n", st[a[i]].number, st[a[i]].num,st[a[i]].name, st[a[i]].c); // 将排序后的数组st中数据写到文件lucky.txt fprintf(fout,"%5d %10d %10s %15s\n", st[a[i]].number, st[a[i]].num,st[a[i]].name, st[a[i]].c); } fclose(fin); fclose(fout); return 0; }
运行结果:
原文:https://www.cnblogs.com/xu9729/p/11070705.html