实验7 文件应用编程

任务4

源代码 

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

 

运行截图

9f292483-8d6a-4e12-9fe7-db36796170f1

 

 

任务5

源代码 

 

  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 
 36     printf("\n通过考试的名单写入文件: 已完成!\n");
 37     write(stu_pass, cnt);
 38 
 39     pass_rate = 1.0 * cnt / N;
 40     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 41     system("pause");
 42     return 0;
 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 
 55 void read(STU st[], int n) {
 56     int i;
 57     FILE *fin;
 58 
 59     fin = fopen("examinee.txt", "r");
 60     if (!fin) {
 61         printf("fail to open file\n");
 62         return;
 63     }
 64 
 65     for (i = 0; i < n; i++)
 66         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 67 
 68     fclose(fin);
 69 }
 70 
 71 
 72 int process(STU st[], int n, STU st_pass[]) {
 73     int i, cnt = 0;
 74     for (i = 0; i < n; i++) {
 75         st[i].sum = st[i].objective + st[i].subjective;
 76         if (st[i].sum >= 60){
 77             strcpy(st[i].result, "通过");
 78             st_pass[cnt] = st[i];
 79             cnt++;}
 80         else 
 81             strcpy(st[i].result, "不通过");
 82        
 83     }
 84     return cnt;
 85 }
 86 
 87 
 88 
 89 void write(STU st[], int n) {
 90     FILE *fout;
 91     int i;
 92     fout = fopen("list_pass.txt", "w");
 93     if (fout == NULL) {
 94         printf("fail to open file to write\n");
 95         return;
 96     }
 97     fprintf(fout, "%-10s%-8s%-12s%-12s%-8s%-8s\n", 
 98             "准考证号", "姓名", "客观题得分", "操作题得分", "总分", "结果");
 99     for (i = 0; i < n; i++) {
100         fprintf(fout, "%-10ld%-8s%-12.2f%-15.2f%-10.2f%-8s\n",
101                 st[i].id, st[i].name, st[i].objective,
102                 st[i].subjective, st[i].sum, st[i].result);
103     }
104     fclose(fout);
105 }
View Code

 

运行截图

 1d7bd723-eff8-41fe-93cb-91b0c5a66e87

9ad35413-7fcc-4673-bf50-14cf68b42712

 

 

任务6

源代码 

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <time.h>
 5 
 6 #define N 100
 7 #define WIN_NUM 5
 8 
 9 typedef struct {
10     char id[20];
11     char name[50];
12     char cls[100];
13 } Stu;
14 
15 int readFile(Stu s[], char fn[]);
16 void lottery(int total, int win[]);
17 void printWin(Stu s[], int win[]);
18 void saveFile(Stu s[], int win[], char fn[]);
19 
20 int main() {
21     Stu s[N];
22     int win[WIN_NUM];
23     int total;
24     char fn[50];
25 
26     total = readFile(s, "list.txt");
27 
28     lottery(total, win);
29     printWin(s, win);
30 
31     printf("\n请输入保存文件名:");
32     scanf("%s", fn);
33     saveFile(s, win, fn);
34     printf("保存成功!\n");
35     system("pause");
36     return 0;
37 }
38 
39 int readFile(Stu s[], char fn[]) {
40     FILE *fp;
41     int n = 0;
42 
43     fp = fopen(fn, "r");
44     while (fscanf(fp, "%s%s%s", s[n].id, s[n].name, s[n].cls) != EOF) {
45         n++;
46         if (n >= N) break;
47     }
48     fclose(fp);
49     return n;
50 }
51 
52 void lottery(int total, int win[]) {
53     int flag[N] = {0};
54     int i, idx;
55 
56     srand((unsigned)time(NULL));
57 
58     for (i = 0; i < WIN_NUM; ) {
59         idx = rand() % total;
60         if (flag[idx] == 0) {
61             win[i] = idx;
62             flag[idx] = 1;
63             i++;
64         }
65     }
66 }
67 
68 void printWin(Stu s[], int win[]) {
69     int i;
70     printf("\n------------ 中奖名单------------\n");
71     for (i = 0; i < WIN_NUM; i++) {
72         printf("%s\t%s\t%s\n", s[win[i]].id, s[win[i]].name, s[win[i]].cls);
73     }
74     printf("\n-----------保存到文件------------\n");
75 }
76 
77 void saveFile(Stu s[], int win[], char fn[]) {
78     FILE *fp;
79     int i;
80 
81     fp = fopen(fn, "w");
82     for (i = 0; i < WIN_NUM; i++) {
83         fprintf(fp, "%s\t%10s\t%20s\n", s[win[i]].id, s[win[i]].name, s[win[i]].cls);
84     }
85     fclose(fp);
86 }
View Code

 

运行截图

 4c382cef-b34c-452f-ab8d-bf0b367b51e1

8f52c41b-ce95-4c66-83ed-b7d6ea501a5d

 

实验总结

1.在任务4中,我用绝对路径和相对路径分别编写了一遍,在相对路径的编释中,未把data4.txt文件移动到项目的根目录(项目名.vcxproj)里,导致程序找不到相应文件。

 

posted @ 2026-06-21 17:24  wf-585  阅读(16)  评论(0)    收藏  举报