实验7_文件应用编程

一、实验目的

1、知道 C 语言中文件处理方式,能区分文本文件和二进制文件

2、会打开 / 关闭文件,能够对文件进行读 / 写操作

3、能综合应用结构体、数组、函数、文件进行应用编程

二、实验过程

1、实验任务4

源代码:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 
 4 int main() {
 5     FILE* fp;
 6     char ch;
 7     int line_count = 0;
 8     int char_count = 0;
 9     int prev_char = '\n';  // 用于判断是否新行开始
10 
11     fp = fopen("data4.txt", "r");
12     if (fp == NULL) {
13         printf("无法打开文件\n");
14         return 1;
15     }
16 
17     while ((ch = fgetc(fp)) != EOF) {
18         // 统计行数:检测换行符
19         if (ch == '\n') {
20             line_count++;
21         }
22 
23         // 统计字符数,排除空白字符(空格、回车、tab)
24         if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r') {
25             char_count++;
26         }
27 
28         prev_char = ch;
29     }
30     // 如果文件最后一行没有换行符,行数加1
31     if (prev_char != '\n') {
32         line_count++;
33     }
34 
35     fclose(fp);
36 
37     printf("data4.txt统计结果:\n");
38     printf("行数:%d\n", line_count);
39     printf("字符数(不计空白符):%d\n", char_count);
40 
41     return 0;
42 }
task4.c

运行结果:

 2、实验任务5

源代码:

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

运行结果:

 3、实验任务6

源代码:

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

运行结果:

posted @ 2025-06-08 23:18  Meredith_GISer  阅读(13)  评论(0)    收藏  举报