实验7 文件应用编程
实验任务4:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 FILE* fp = fopen("data4.txt", "r"); 6 if (fp == NULL) 7 { 8 printf("文件打开失败!\n"); 9 return -1; 10 } 11 char ch; 12 int line = 0; 13 int charCnt = 0; 14 int lastIsLine = 0; 15 while ((ch = fgetc(fp)) != EOF) 16 { 17 if (ch == '\n' && lastIsLine == 0) 18 { 19 line++; 20 lastIsLine = 1; 21 } 22 else 23 { 24 charCnt++; 25 lastIsLine = 0; 26 } 27 } 28 if (lastIsLine == 0) 29 line++; 30 fclose(fp); 31 printf("data4.txt统计结果:\n"); 32 printf("行数: %d\n", line); 33 printf("字符数(不计空白符): %d\n", charCnt); 34 system("pause"); 35 return 0; 36 }
运行截图:

实验任务5:
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #define N 10 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 void read(STU st[], int n); 15 int process(STU st[], int n, STU st_pass[]); 16 void write(STU st[], int n); 17 void output(STU st[], int n); 18 int main() 19 { 20 STU stu[N], stu_pass[N]; 21 int passCount; 22 double passRate; 23 read(stu, N); 24 printf("成功读取%d位考生信息\n\n", N); 25 passCount = process(stu, N, stu_pass); 26 printf("===== 全部考生成绩表 =====\n"); 27 output(stu, N); 28 write(stu_pass, passCount); 29 printf("\n合格考生已保存至 list_pass.txt\n"); 30 passRate = 1.0 * passCount / N; 31 printf("本次考试通过率:%.2f%%\n", passRate * 100); 32 system("pause"); 33 return 0; 34 } 35 void read(STU st[], int n) 36 { 37 FILE* fp = fopen("examinee.txt", "r"); 38 if (fp == NULL) 39 { 40 printf("examinee.txt 文件打开失败!请放到Project26根目录\n"); 41 system("pause"); 42 exit(-1); 43 } 44 for (int i = 0; i < n; i++) 45 { 46 fscanf(fp, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 47 } 48 fclose(fp); 49 } 50 int process(STU st[], int n, STU st_pass[]) 51 { 52 int num = 0; 53 for (int i = 0; i < n; i++) 54 { 55 st[i].sum = st[i].objective + st[i].subjective; 56 if (st[i].sum >= 60) 57 { 58 strcpy(st[i].result, "通过"); 59 st_pass[num++] = st[i]; 60 } 61 else 62 { 63 strcpy(st[i].result, "未通过"); 64 } 65 } 66 return num; 67 } 68 void write(STU st[], int n) 69 { 70 FILE* fp = fopen("list_pass.txt", "w"); 71 if (fp == NULL) 72 { 73 printf("文件写入失败!\n"); 74 return; 75 } 76 fprintf(fp, "%-8s %-12s %-6s %-6s %-6s %s\n", "准考证", "姓名", "客观分", "操作分", "总分", "结果"); 77 for (int i = 0; i < n; i++) 78 { 79 fprintf(fp, "%-8ld %-12s %-6.1f %-6.1f %-6.1f %s\n", 80 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 81 } 82 fclose(fp); 83 } 84 void output(STU st[], int n) 85 { 86 printf("%-8s %-12s %-6s %-6s %-6s %s\n", "准考证", "姓名", "客观", "操作", "总分", "结果"); 87 for (int i = 0; i < n; i++) 88 { 89 printf("%-8ld %-12s %-6.1f %-6.1f %-6.1f %s\n", 90 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 91 } 92 }
运行截图:


实验任务6:
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #include <string.h> 6 #define MAX_STU 80 7 #define LEN 50 8 typedef struct { 9 char id[20]; 10 char name[20]; 11 char cls[30]; 12 } Student; 13 int main() 14 { 15 Student stu[MAX_STU]; 16 int flag[MAX_STU] = { 0 }; 17 int count = 0; 18 FILE* fp = fopen("list.txt", "r"); 19 if (!fp) 20 { 21 printf("list.txt打开失败\n"); 22 system("pause"); 23 return -1; 24 } 25 while (fscanf(fp, "%s %s %s", stu[count].id, stu[count].name, stu[count].cls) == 3) 26 { 27 count++; 28 } 29 fclose(fp); 30 srand((unsigned)time(NULL)); 31 Student win[5]; 32 int winCnt = 0; 33 while (winCnt < 5) 34 { 35 int idx = rand() % count; 36 if (flag[idx] == 0) 37 { 38 flag[idx] = 1; 39 win[winCnt++] = stu[idx]; 40 } 41 } 42 printf("--------中奖名单--------\n"); 43 for (int i = 0; i < 5; i++) 44 { 45 printf("%s %s %s\n", win[i].id, win[i].name, win[i].cls); 46 } 47 char filename[50]; 48 printf("输入保存文件名:"); 49 scanf("%s", filename); 50 FILE* fw = fopen(filename, "w"); 51 if (!fw) 52 { 53 printf("文件保存失败!\n"); 54 system("pause"); 55 return -1; 56 } 57 for (int i = 0; i < 5; i++) 58 { 59 fprintf(fw, "%s %s %s\n", win[i].id, win[i].name, win[i].cls); 60 } 61 fclose(fw); 62 printf("保存成功!\n"); 63 system("pause"); 64 return 0; 65 }
运行截图:

浙公网安备 33010602011771号