四、实验结论

task4.c

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main(){
 5     FILE *fp;
 6     int count_line=0;
 7     int count_str=0;
 8     char ch;
 9     fp=fopen("D:\\data4.txt","r");
10     if(!fp){
11         printf("fail to open file to read\n");
12         system("pause");
13         return 0;
14     }
15     while((ch=fgetc(fp))!=EOF){
16         if(ch=='\n')
17             count_line++;
18         if(ch!=' '&&ch!='\t'&&ch!='\n')
19             count_str+=1;
20     }
21     fclose(fp);
22     count_line++;
23     printf("data4.txt统计结果:\n");
24     printf("行数: %d\n", count_line);
25     printf("字符数(不计空白符): %d\n",count_str);
26 
27     system("pause");
28     return 0;
29 }

 

运行结果截图

屏幕截图 2026-06-19 165822

 

task5.c

源代码

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include<stdlib.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long id;            // 准考证号
  9     char name[20];      // 姓名
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;          // 总分
 13     char result[10];    // 考试结果
 14 } STU;
 15 
 16 // 函数声明
 17 void read(STU st[], int n);
 18 void write(STU st[], int n);
 19 void output(STU st[], int n);
 20 int process(STU st[], int n, STU st_pass[]);
 21 
 22 int main() {
 23     STU stu[N], stu_pass[N];
 24     int cnt;
 25     double pass_rate;
 26 
 27     printf("从文件读入%d个考生信息...\n", N);
 28     read(stu, N);
 29 
 30     printf("\n对考生成绩进行统计...\n");
 31     cnt = process(stu, N, stu_pass);
 32 
 33     printf("\n通过考试的名单:\n");
 34     output(stu, N);   // 输出所有考生完整信息到屏幕
 35     write(stu, N);    // 输出考试通过的考生信息到文件
 36 
 37     pass_rate = 1.0 * cnt / N;
 38     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 39 
 40     system("pause");
 41     return 0;
 42 }
 43 
 44 // 把所有考生完整信息输出到屏幕上
 45 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 46 void output(STU st[], int n) {
 47     int i;
 48     
 49     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 50     for (i = 0; i < n; i++)
 51         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
 52 }
 53 
 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 55 void read(STU st[], int n) {
 56     int i;
 57     FILE *fin;
 58 
 59     fin = fopen("F:\\examinee.txt", "r");
 60     if (!fin) {
 61         printf("fail to open file\n");
 62         return;
 63     }
 64 
 65     while (!feof(fin)) {
 66         for (i = 0; i < n; i++)
 67             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 68     }
 69 
 70     fclose(fin);
 71 }
 72 
 73 // 把通过考试的考生完整信息写入文件list_pass.txt
 74 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 75 void write(STU s[], int n) {
 76     FILE *fp;
 77     int i,number;
 78 
 79     fp=fopen("F:\\list_pass.txt","w");
 80     if(!fp){
 81         printf("fail to open file to write");
 82         system("pause");
 83         return;
 84     }
 85     fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 86     for(i=0;i<n;i++)
 87         if(s[i].sum>=60){
 88             number=fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
 89             if(number != 6)
 90                 break;
 91         }
 92     fclose(fp);
 93     }
 94 
 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 96 int process(STU st[], int n, STU st_pass[]) {
 97     int cnt=0,i;
 98     for(i=0;i<n;i++){
 99         st[i].sum=st[i].objective + st[i].subjective;
100         if(st[i].sum>=60){
101                 strcpy(st[i].result,"通过");
102                 st_pass[cnt]=st[i];
103                 cnt++;
104         }
105             
106         else{
107             strcpy(st[i].result,"未通过");
108         }
109     }
110     return cnt;
111 }

 

运行结果截图

屏幕截图 2026-06-19 170034

屏幕截图 2026-06-19 170052

 

task6.c

源代码

 1 #include<stdlib.h>
 2 #include<stdio.h>
 3 #include<time.h>
 4 #define N 80
 5 
 6 int main(){
 7     char info[N][80];
 8     int i;
 9     char filename[N];
10     int lucky_index;
11 
12     FILE *fp;
13     fp=fopen("D:\\list.txt","r");
14     if(!fp){
15         printf("fail to open file to read\n");
16         system("pause");
17         return 1;
18 }
19     for(i=0;i<N;++i)
20         fgets(info[i],80,fp);
21     fclose(fp);
22     printf("----------------保存到文件-----------------\n");
23     printf("输入文件名: ");
24     scanf("%s",&filename);
25     fp = fopen(filename, "a");
26     srand(time(NULL));
27     printf("----------------中奖名单-----------------\n");
28     for(i=0;i<5;++i){
29         lucky_index=rand()% N;
30         printf("%s",info[lucky_index]);
31         fprintf(fp, "%s\n",info[lucky_index]);
32     }
33 
34     fclose(fp);
35     printf("文件保存成功!\n");
36 
37     system("pause");
38     return 0;
39 }

 

运行结果截图

屏幕截图 2026-06-19 170331

屏幕截图 2026-06-19 170344

五、实验总结

实验1和实验2对比很直观:文本文件用记事本打开能看到内容,二进制文件打开是乱码。文本文件适合人类阅读,二进制文件存储效率更高、占用空间更小。每次写文件都记得:fopen → 读写操作 → fclose。之前经常忘关文件,这次刻意注意了。打开文件后一定要判断fp == NULL,否则文件不存在时程序会崩溃。综合应用能力提高了,实验5把文件读写和结构体、数组、函数结合起来,从文件读数据→处理数据→写回文件,完成了一个完整的数据处理流程。实验6的随机抽奖用到了随机数、去重、文件写入,感觉把前面学的东西都串起来了。