实验5 结构体和文件

四、实验结论
1. 实验任务1
task1_1
 1 //将图书信息写入文本文件data1.txt
 2  
 3 #include <stdio.h>
 4 #define N 5
 5 #define M 80
 6 
 7 typedef struct
 8 {
 9     char name[M]; // 书名
10     char author[M]; // 作者
11 }Book;
12 
13 
14 int main()
15 {
16     Book x[N] = {     {"一九八四", "乔治.奥威尔"},
17                       {"美丽新世界", "赫胥黎"},
18                       {"昨日的世界", "斯蒂芬.茨威格"},
19                       {"万历十五年", "黄仁宇"},
20                       {"一只特立独行的猪", "王小波"}
21                 };    
22     int i;
23         
24     FILE *fp;
25     
26     // 以写的方式打开文本文件data1.txt
27     fp = fopen("data1.txt", "w");
28     
29     // 如果打开文件失败,输出提示信息并返回
30     if(fp == NULL)
31     {
32         printf("fail to open file\n");
33         return 1;
34     }
35     
36     // 将结构体数组x中的图书信息写到fp指向的文件data1.txt
37     // 同时也输出到屏幕上
38     for(i=0; i<N; ++i)
39     {
40         fprintf(fp, "%-20s %-20s\n", x[i].name, x[i].author);
41         printf("%-20s %-20s\n", x[i].name, x[i].author);
42     }
43     
44     fclose(fp);
45     
46     return 0;
47 }

task1_2
 1 //从文本文件data1.txt中读取图书信息,并打印输出到屏幕 
 2 
 3 #include<stdio.h>
 4 #define N 5
 5 #define M 80
 6 
 7 typedef struct
 8 {
 9     char name[M]; // 书名
10     char author[M]; // 作者
11 }Book;
12 
13 
14 int main()
15 {
16     Book x[N];
17     int i;
18     
19     FILE *fp;
20     
21     // 以读的方式打开文本文件data1.txt
22     fp = fopen("data1.txt", "r");
23     
24     // 如果打开文件失败,输出提示信息并返回
25     if(fp == NULL)
26     {
27         printf("fail to open file\n");
28         return 1;
29     }
30     
31     // 从fp指向的文件data1.txt中读取信息到结构体数组x
32     // 同时,把x的内容输出到屏幕上
33     for(i=0; i<N; ++i)
34     {
35         fscanf(fp, "%s %s\n", x[i].name, x[i].author);
36         printf("%-20s %-20s\n", x[i].name, x[i].author);
37     }
38     
39     fclose(fp);
40     
41     return 0;
42 }

问题:line35,从文件中读取图书名、图书作者时,为什么x[i].name和x[i].author前面没有地址符&?

回答:name和author是字符数组,数组不需要加取地址符获取的也是该元素的地址。

2. 实验任务2
task2_1
 1 //将图书信息以数据块方式写入二进制文件data2.dat
 2 
 3 #include <stdio.h>
 4 #define N 5
 5 #define M 80
 6 
 7 typedef struct
 8 {
 9     char name[M]; // 书名
10     char author[M]; // 作者
11 }Book;
12 
13 
14 int main()
15 {
16     Book x[N] = { {"一九八四", "乔治.奥威尔"},
17                   {"美丽新世界", "赫胥黎"},
18                   {"昨日的世界", "斯蒂芬.茨威格"},
19                   {"万历十五年", "黄仁宇"},
20                   {"一只特立独行的猪", "王小波"}
21                 };
22     int i;
23     
24     FILE *fp;
25     
26     // 以写的方式打开二进制文件data2.dat
27     fp = fopen("data2.dat", "wb");
28     
29     // 如果打开文件失败,输出提示信息并返回
30     if(fp == NULL)
31     {
32         printf("fail to open file\n");
33         return 1;
34     }
35     
36     // 将结构体数组x中的图书信息写以数据块方式写入文件
37     // 把从地址x处开始sizeof(Book)×N个字节大小的数据块写入fp指向的文件
38     fwrite(x, sizeof(Book), N, fp);
39     
40     
41     fclose(fp);
42     
43     return 0;
44 }

问题:1. 观察、验证在当前路径下,是否生成一个文件名为data2.dat的数据文件。
   2. 尝试用文本编辑器打开,查看其内容,是否直观可见?
回答:1.生成了
   2.用记事本打开,内容可见,以一行一列的形式展现
 
task2_2
 1 //从二进制文件data2.dat中读取图书信息,并打印输出到屏幕
 2 
 3 #include <stdio.h>
 4 #define N 5
 5 #define M 80
 6 
 7 typedef struct
 8 {
 9     char name[M]; // 书名
10     char author[M]; // 作者
11 }Book;
12 
13 
14 int main()
15 {
16     Book x[N];
17     int i;
18     
19     FILE *fp;
20     
21     // 以读的方式打开二进制文件data2.dat
22     fp = fopen("data2.dat", "rb");
23     
24     // 如果打开文件失败,输出提示信息并返回
25     if(fp == NULL)
26     {
27         printf("fail to open file\n");
28         return 1;
29     }
30     
31     // 从fp指向的文件中读取数据块到x对应的地址单元
32     // 数据块大小为sizeof(Book)×N
33     fread(x, sizeof(Book), N, fp);
34     
35     // 在屏幕上输出结构体数组x中保存的数据
36     for(i=0; i<N; ++i)
37         printf("%-20s%-20s\n", x[i].name, x[i].author);
38         
39         
40     fclose(fp);
41     
42     return 0;
43 }

3. 实验任务3
 1 //统计数据文件data3_1.txt中字符数 
 2 
 3 #include <stdio.h>
 4 
 5 int main()
 6 {
 7     FILE *fin, *fout;
 8     char ch;
 9     int count=0;
10     
11     // 以只读、文本方式打开文件data3_1.txt
12     fin = fopen("data3_1.txt", "r");
13     
14     // 如果打开失败,输出提示信息并返回
15     if(fin == NULL)
16     {
17         printf("fail to open data3_1.txt\n");
18         return 1;
19     }
20     
21     // 当fin指向的文件data1_txt没有结束时
22     while( !feof(fin) )
23     {
24         // 从fin指向的文件data1_txt读取单个字符
25         ch = fgetc(fin);
26         
27         // 如果ch不是空白符,个数加一 
28         if(ch != ' ' && ch!='\t'&&ch!='\n')
29             count+=1;
30     }
31     
32     fclose(fin);
33     printf("data3_1.txt中包含字符数:%d",count); 
34     
35     return 0;
36 }

5. 实验任务5
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 #define N 10
  6 
  7 typedef struct
  8 {
  9     long int id;
 10     char name[20];
 11     float objective; /*客观题得分*/
 12     float subjective; /*操作题得分*/
 13     float sum;
 14     char level[10];
 15 } STU;
 16 
 17 // 函数声明
 18 void input(STU s[], int n);
 19 void output(STU s[], int n);
 20 void process(STU s[], int n);
 21 
 22 int main()
 23 {
 24     STU stu[N];
 25     printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
 26     input(stu, N);
 27     
 28     printf("\n对考生信息进行处理: 计算总分,确定等级\n");
 29     process(stu, N);
 30     
 31     printf("\n打印考生完整信息, 并保存到文件中");
 32     output(stu, N);
 33     
 34     return 0;
 35 }
 36 
 37 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 38 void input(STU s[], int n)
 39 {
 40     int i;
 41     FILE *fin;
 42     fin = fopen("examinee.txt", "r");
 43     if (fin == NULL)
 44     {
 45         printf("fail to open file\n");
 46         exit(0);
 47     }
 48     
 49     while (!feof(fin))
 50     {
 51         for (i = 0; i < n; i++)
 52             fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name,&s[i].objective, &s[i].subjective);
 53     }
 54     
 55     fclose(fin);
 56 }
 57 
 58 //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
 59 // 不仅输出到屏幕上,还写到文本文件result.txt中
 60 void output(STU s[], int n)
 61 {
 62     FILE *fout;
 63     int i;
 64     
 65     // 输出到屏幕
 66     printf("\n");
 67     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 68     for (i = 0; i < n; i++)
 69         printf("%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].level);
 70     
 71     // 保存到文件
 72     fout = fopen("result.txt", "w");
 73     if (!fout)
 74     {
 75         printf("fail to open or create result.txt\n");
 76         exit(0);
 77     }
 78     
 79     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 80     
 81     for (i = 0; i < n; i++)
 82         fprintf(fout, "%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].level);
 83         
 84     fclose(fout);
 85 }
 86 
 87 // 对考生信息进行处理:计算总分,排序,确定等级
 88 void process(STU s[], int n)
 89 {
 90     int i = 0,j;
 91     float leva = n / 10,levb=n/2;
 92     STU c;
 93     for (i = 0; i < n; i++) {
 94         s[i].sum = s[i].objective + s[i].subjective;        
 95     }
 96     for (i=0;i<n;i++)
 97         for ( j = i; j < n; j++)
 98         {
 99             if (s[i].sum < s[j].sum) {
100                 c = s[i];
101                 s[i] = s[j];
102                 s[j] = c;
103             }
104         }    
105     for (i = 0; i < n; i++) {
106         if (i<leva)
107             strcpy(s[i].level, "优秀");
108         else if 
109             (i < levb)strcpy(s[i].level, "合格");
110         else 
111             strcpy(s[i].level, "不合格");
112     }
113 }

6. 实验任务6
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 #define N 80
 6 typedef struct
 7 {
 8     long int id;
 9     char name[20];
10     char class[80];
11     int target;
12 } STU;
13 void input(STU s[], int n);
14 void srand_stu(STU s[], STU lucky[], int num);
15 void output(STU s[], int n);
16 int main()
17 {
18     STU stu[N];
19     STU lucky_stu[5];
20     //导入数据到stu数组
21     input(stu, N);
22     //选人
23     srand_stu(stu, lucky_stu, 5);
24     return 0;
25 }
26 void input(STU s[], int n)
27 {
28     int i;
29     FILE *fin;
30     fin = fopen("list.txt", "r");
31     if (fin == NULL)
32     {
33         printf("fail to open file\n");
34         exit(0);
35     }
36     while (!feof(fin))
37     {
38         for (i = 0; i < n; i++)
39             fscanf(fin, "%ld %s %s", &s[i].id, s[i].name,
40                    &s[i].class);
41     }
42     fclose(fin);
43 }
44 void srand_stu(STU s[], STU lucky[], int num)
45 {
46     int i;
47     time_t t;
48     srand((unsigned)time(&t));
49     int lucky_num;
50     for ( i = 0; i < num; i++)
51     {
52         lucky_num = rand() % 79 + 1;
53         if (s[lucky_num].target != 1)
54         {
55             lucky[i] = s[lucky_num];
56             s[lucky_num].target = 1;
57         }
58     }
59     for (i = 0; i < 5; i++)
60     {
61         printf("%ld  %s  %s\n", lucky[i].id, lucky[i].name, lucky[i].class);
62     }
63     FILE *fout;
64     fout = fopen("result.txt", "w");
65     for (i = 0; i < num; i++)
66         fprintf(fout, "%ld  %s  %s\n", lucky[i].id, lucky[i].name, lucky[i].class);
67     fclose(fout);
68 }

 

posted @ 2022-05-31 16:04  liuxianer  阅读(46)  评论(0)    收藏  举报