实验7

TASK 1

 1 #include<stdio.h>
 2 #define N 80
 3 #define M 100
 4 
 5 typedef struct
 6 {
 7     char name[N];
 8     char author[N];
 9 }book;
10 
11 void write();
12 void read();
13 
14 int main()
15 {
16     printf("测试1:把图书信息写入文本文件\n");
17     write();
18     
19     printf("\n测试2:从文本文件读取图书信息,打印输出到屏幕\n");
20     read();
21     
22     return 0;
23 }
24 
25 void write()
26 {
27     book x[]=
28     {
29         {"《雕塑家》", "斯科特.麦克劳德"},
30         {"《灯塔》", "克里斯多夫.夏布特"},
31         {"《人的局限性》", "塞缪尔.约翰生"}, 
32         {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"},
33         {"《大地之上》", "罗欣顿·米斯特里"}, 
34         {"《上学记》", "何兆武"}, 
35         {"《命运》", "蔡崇达"} 
36     };
37     int n,i;
38     FILE *fp;
39     
40     n=sizeof(x)/sizeof(x[0]);
41     fp=fopen("data1.txt","w");
42     
43     if(fp==NULL)
44     {
45         printf("fail to open file to write\n");
46         return;
47     }
48     
49     for(i=0;i<n;++i)
50         fprintf(fp,"%-40s %-20s\n",x[i].name,x[i].author);
51         
52     fclose(fp);
53 }
54 
55 void read()
56 {
57     book x[M];
58     int i,n;
59     int number;
60     
61     FILE *fp;
62     fp=fopen("data1.txt","r");
63     
64     if(fp==NULL)
65     {
66         printf("fail to open file to read\n");
67         return;
68     }
69     
70     i=0;
71     while(!feof(fp))
72     {
73         number=fscanf(fp,"%s%s",x[i].name,x[i].author);
74         if(number!=2)
75             break;
76         i++;
77     }
78     n=i;
79     
80     for(i=0;i<n;++i)
81         printf("%d.%-40s%-20s\n",i+1,x[i].name,x[i].author);
82         
83     fclose(fp);
84 }

问题:加上判断语句可以确保完整的两条数据被读取了

TASK 2

 1 #include<stdio.h>
 2 #define N 80
 3 #define M 100
 4 
 5 typedef struct
 6 {
 7     char name[N];
 8     char author[N];
 9 }book;
10 
11 void write();
12 void read();
13 
14 int main()
15 {
16     printf("测试1:把图书信息以数据块方式写入二进制文件\n");
17     write();
18     
19     printf("\n测试2:从二进制文件读取图书信息,打印输出到屏幕\n");
20     read();
21     
22     return 0;
23 }
24 
25 void write()
26 {
27     book x[]=
28     {
29         {"《雕塑家》", "斯科特.麦克劳德"},
30         {"《灯塔》", "克里斯多夫.夏布特"},
31         {"《人的局限性》", "塞缪尔.约翰生"}, 
32         {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"},
33         {"《大地之上》", "罗欣顿·米斯特里"}, 
34         {"《上学记》", "何兆武"}, 
35         {"《命运》", "蔡崇达"} 
36     };
37     int n;
38     FILE *fp;
39     
40     n=sizeof(x)/sizeof(x[0]);
41     fp=fopen("data2.dat","wb");
42     
43     if(fp==NULL)
44     {
45         printf("fail to open file to write\n");
46         return;
47     }
48     
49     fwrite(x,sizeof(book),n,fp);
50     
51     fclose(fp);
52 }
53 
54 void read()
55 {
56     book x[M];
57     int i,n;
58     int number;
59     
60     FILE *fp;
61     fp=fopen("data2.dat","rb");
62     
63     if(fp==NULL)
64     {
65         printf("fail to open file to read\n");
66         return;
67     }
68     
69     i=0;
70     while (!feof(fp))
71     {
72         number=fread(&x[i],sizeof(book),1,fp);
73         if(number!=1)
74             break;
75         i++;
76     }
77     
78     n=i;
79     for(i=0;i<n;++i)
80         printf("%d.%-40s%-20s\n",i+1,x[i].name,x[i].author);
81         
82     fclose(fp);
83 }

问题:去掉之后会多出来一个第八行,计算机必须在发现第八行输出为空后才终止,加上两行代码后可以在输入的时候就判断是否全部完成

TASK 3

 1 #include<stdio.h>
 2 #define N 5
 3 #define M 80
 4 
 5 void write();
 6 void read_str();
 7 void read_char();
 8 
 9 int main()
10 {
11     printf("测试1:把一组字符信息以字符串方式写入文本文件\n");
12     write();
13     
14     printf("\n测试2:从文件以字符串方式读取,输出到屏幕\n");
15     read_str();
16     
17     printf("\n测试3:从文件以单个字符方式读取,输出到屏幕\n");
18     read_char();
19     
20     return 0;
21 }
22 
23 void write()
24 {
25     char *ptr[N]=
26     {
27         "Working\'s Blues",
28         "Everything Will Flow",
29         "Streets of London",
30         "Perfect Day",
31         "Philadelphia"
32     };
33     int i;
34     FILE *fp;
35     
36     fp=fopen("data3.txt","w");
37     if(!fp)
38     {
39         printf("fail to open file to write\n");
40         return;
41     }
42     
43     for(i=0;i<N;++i)
44     {
45         fputs(ptr[i],fp);
46         fputs("\n",fp);
47     }
48     
49     fclose(fp);
50 }
51 
52 void read_str()
53 {
54     char songs[N][M];
55     int i;
56     FILE *fp;
57     
58     fp=fopen("data3.txt","r");
59     if(!fp)
60     {
61         printf("fail to open file to read\n");
62         return;
63     }
64     
65     for(i=0;i<N;++i)
66         fgets(songs[i],80,fp);
67     for(i=0;i<N;++i)
68         printf("%d.%s",i+1,songs[i]);
69         
70     fclose(fp); 
71 }
72 
73 void read_char()
74 {
75     char ch;
76     FILE *fp;
77     
78     fp=fopen("data3.txt","r");
79     if(!fp)
80     {
81         printf("fail to open file to read\n");
82         return;
83     }
84     while(!feof(fp))
85     {
86         ch=fgetc(fp);
87         if(ch==EOF)
88             break;
89         putchar(ch);
90     }
91     fclose(fp);
92 }

问题:\'表示’字符,避免编译的时候被理解为其他东西

TASK 4

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define N 100
 4 #define M 80 
 5 
 6 void read();
 7 
 8 int main()
 9 {
10     printf("data4.txt统计结果:\n");
11     read();
12     return 0; 
13 }
14 
15 void read()
16 {
17     int i,n,j,k,sum=0;
18     char x[N][M];
19     int number;
20     
21     FILE *fp;
22     
23     fp = fopen("data4.txt", "r");
24     
25     if(fp==NULL)
26     {
27         printf("fail to open file to read\n");    
28         return;    
29     }
30     i=0;
31     
32     while(!feof(fp))
33     {
34     
35         fgets(x[i], 80, fp);
36         i++;
37     }
38     printf("行数:%d\n",i);
39     
40     for(j=0;j<i;j++)
41     {
42         n=strlen(x[j]);
43         for(k=0;k<n;k++)
44         if(x[j][k]!=' ' && x[j][k]!='\n')sum++;
45     }
46     printf("字符数:%d\n",sum); 
47     fclose(fp);
48 }

TASK 5

  1 #include<stdio.h>
  2 #include<string.h>
  3 #define N 10
  4 
  5 typedef struct
  6 {
  7     long id;
  8     char name[20];
  9     float objective;
 10     float subjective;
 11     float sum;
 12     char result[10];
 13 }STU;
 14 
 15 void read(STU st[],int n);
 16 void write(STU st[],int n);
 17 void output(STU st[],int n);
 18 int process(STU st[],int n,STU st_pass[]);
 19 
 20 int main()
 21 {
 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,N);
 34     write(stu,N);
 35     
 36     pass_rate=1.0*cnt/N;
 37     printf("\n本次等级考试通过率:%.2f%%\n",pass_rate*100);
 38     
 39     return 0;
 40 }
 41 
 42 void output(STU st[],int n)
 43 {
 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", 
 48         st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
 49 }
 50 
 51 void read(STU st[],int n)
 52 {
 53     int i;
 54     FILE *fin;
 55     
 56     fin=fopen("examinee.txt","r");
 57     if(!fin)
 58     {
 59         printf("fail to open file\n");
 60         return;
 61     }
 62     while(!feof(fin))
 63     {
 64         for(i=0;i<n;i++)
 65             fscanf(fin,"%ld %s %f %f",&st[i].id,st[i].name,&st[i].objective,&st[i].subjective);
 66     }
 67     fclose(fin);
 68 }
 69 
 70 void write(STU st[], int n) 
 71 {
 72     FILE *fp;
 73     int i;
 74 
 75     fp = fopen("list_pass.txt", "w");
 76     if (fp == NULL) {
 77         printf("fail to open file to write\n");
 78         return;
 79     }
 80 
 81     fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
 82 
 83     for (i = 0; i < n; i++) {
 84         if(st[i].result=="通过")
 85         {
 86         fprintf(fp, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",
 87                 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
 88         }
 89     }
 90 
 91     fclose(fp);
 92 }
 93 
 94 
 95 int process(STU st[], int n, STU st_pass[]) 
 96 {
 97     int i, cnt = 0;
 98     for (i = 0; i < n; i++) 
 99     {
100         st[i].sum = st[i].objective + st[i].subjective;
101         if (st[i].sum >= 60) 
102         {
103             st_pass[cnt] = st[i];
104             cnt++;
105         }
106         strcpy(st[i].result, st[i].sum >= 60 ? "通过" : "不通过");
107     }
108     return cnt;
109 }

TASK 6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 80
 5 
 6 typedef struct student
 7 {
 8     int no;
 9     char name[N];
10     char class[N];
11     int flag;
12 }STU;
13 
14 void write();
15 
16 int main()
17 {
18     STU students[N];
19     int ch[5];
20     FILE *fp;
21     fp=fopen("list.txt","r");
22     
23     if (fp == NULL) 
24     {
25         printf("fail to open file to read\n");
26         return;
27     }
28     
29     int i;
30     for (i = 0; i < N; ++i) 
31     {
32         if (fscanf(fp, "%d %s %c", &students[i].no, students[i].name, &students[i].class) != 3) {
33             break;
34         students[i].flag=0;
35     }
36 
37     fclose(fp);
38     
39     time_t t;
40     srand((unsigned int)time(&t));
41     
42     printf("---------------------随机抽点名单------------------------");
43     
44     for(i=0;i<5;++i)
45     {
46         do
47         {
48             ch[i]=rand()%N;
49         }while(students[ch[i]].flag==1);
50         printf("%d %-20s %-20s\n",students[ch[i]].no,students[ch[i]].name,students[ch[i]].class);
51         write(); 
52         students[ch[i]].flag=1;    
53     }
54     
55     return 0;
56 }
57 
58 void write(STU *students, int ch[], int i) {
59     time_t now = time(NULL);
60 
61     struct tm *local_time = localtime(&now);
62 
63     char filename[20];
64 
65     strftime(filename, sizeof(filename), "%Y%m%d_%H%M%S.txt", local_time); 
66 
67     FILE *fp = fopen(filename, "w");
68     if (fp == NULL) {
69         printf("fail to open file\n");
70         return;
71     }
72 
73     fprintf(fp, "%d %-20s %-20s\n", students[ch[i]].no, students[ch[i]].name, students[ch[i]].class);
74 
75     fclose(fp);
76 }

编译的时候还是会报错,询问kimi之后也没能解决问题(sad)

主要问题在于怎么用时间戳生成文件名

 

总结:

文件这一章和之前学的内容都很不一样,上课讲的时候能完全听懂理解,但自己上手写的时候就很慌乱、甚至无从下手。

我自己感觉,一个比较明显的原因是文件的写入、读取、输出函数和以往的函数规则有所不同,在使用的时候我需要思考我到底要做什么、功能相似的函数到底要用哪一个、怎么和之前的数组指针等等的知识点连接起来,对我来说都是不小的挑战。

posted @ 2024-12-30 07:34  Florence11  阅读(17)  评论(0)    收藏  举报