实验7 文件应用编程
4. 实验任务4:文件简单应用
#include <stdio.h> #include <stdlib.h> void countLinesAndChars(const char *fileName) { FILE *fp; int lines = 0, chars = 0; int ch; int inWord = 0; fp = fopen(fileName, "r"); if (fp == NULL) { printf("无法打开文件 %s\n", fileName); return; } while ((ch = fgetc(fp))!= EOF) { if (ch == '\n') { lines++; inWord = 0; } else if (ch!=' ' && ch!= '\t') { if (!inWord) { chars++; inWord = 1; } } else { inWord = 0; } } // 处理最后一行没有换行符的情况 if (inWord) { lines++; } fclose(fp); printf("%s统计结果:\n", fileName); printf("行数:%d\n", lines); printf("字符数(不计空白符):%d\n", chars); } int main() { countLinesAndChars("data4.txt"); return 0; }
5. 实验任务5:文件综合应用
#include <stdio.h> #include <string.h> #define N 10 typedef struct { long id; char name[20]; float objective; float subjective; float sum; char result[10]; } STU; // 函数声明 void read(STU st[], int n); void write(STU st[], int n); void output(STU st[], int n); int process(STU st[], int n, STU st_pass[]); int main() { STU stu[N], stu_pass[N]; int cnt; double pass_rate; printf("从文件读入%d个考生信息...\n", N); read(stu, N); printf("\n对考生成绩进行统计...\n"); cnt = process(stu, N, stu_pass); printf("\n通过考试的名单:\n"); output(stu, N); // 输出所有考生完整信息到屏幕 write(stu_pass, cnt); // 输出考试通过的考生信息到文件 pass_rate = 1.0 * cnt / N; printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); return 0; } // 把所有考生完整信息输出到屏幕上 // 准考证号, 姓名, 客观题得分, 操作题得分, 总分, 结果 void output(STU st[], int n) { int i; printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); for (i = 0; i < n; i++) printf("%ld\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); } // 从文本文件examinee.txt读入考生信息:准考证号, 姓名, 客观题得分, 操作题得分 void read(STU st[], int n) { int i; FILE *fin; fin = fopen("examinee.txt", "r"); if (!fin) { printf("fail to open file\n"); return; } i = 0; while (i < n && fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective) == 4) { i++; } fclose(fin); } // 把通过考试的考生完整信息写入文件list_pass.txt // 准考证号, 姓名, 客观题得分, 操作题得分, 总分, 结果 void write(STU s[], int n) { int i; FILE *fout; fout = fopen("list_pass.txt", "w"); if (!fout) { printf("fail to open file for writing\n"); return; } fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); for (i = 0; i < n; i++) { fprintf(fout, "%ld\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].result); } fclose(fout); } // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 int process(STU st[], int n, STU st_pass[]) { int i, cnt = 0; for (i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if (st[i].sum >= 60) { strcpy(st[i].result, "通过"); st_pass[cnt++] = st[i]; } else { strcpy(st[i].result, "不通过"); } } return cnt; }
6. 实验任务6:文件综合应用
#include <stdio.h> #include <stdlib.h> #include <time.h> #define STUDENT_COUNT 80 #define SELECT_COUNT 5 typedef struct { long id; char name[20]; char class[30]; } Student; void read(Student students[]) { FILE *fp; int i = 0; fp = fopen("list.txt", "r"); if (fp == NULL) { printf("无法打开文件list.txt\n"); return; } while (fscanf(fp, "%ld %s %s", &students[i].id, students[i].name, students[i].class)!= EOF && i < STUDENT_COUNT) { i++; } fclose(fp); } int random(int max) { return rand() % max; } int isSelected(int selected[], int index) { for (int i = 0; i < SELECT_COUNT; i++) { if (selected[i] == index) { return 1; } } return 0; } void select(Student students[], Student selectedStudents[]) { int selected[SELECT_COUNT] = {0}; int count = 0; srand((unsigned int)time(NULL)); while (count < SELECT_COUNT) { int index = random(STUDENT_COUNT); if (!isSelected(selected, index)) { selected[count] = index; selectedStudents[count] = students[index]; count++; } } } void display(Student selectedStudents[]) { printf("--------------随机抽点名单--------------\n"); for (int i = 0; i < SELECT_COUNT; i++) { printf("%ld %s %s\n", selectedStudents[i].id, selectedStudents[i].name, selectedStudents[i].class); } } void write(Student selectedStudents[]) { char fileName[30]; FILE *fp; printf("--------------保存到文件--------------\n"); printf("输入文件名:"); scanf("%s", fileName); fp = fopen(fileName, "w"); if (fp == NULL) { printf("无法创建文件%s\n", fileName); return; } for (int i = 0; i < SELECT_COUNT; i++) { fprintf(fp, "%ld %s %s\n", selectedStudents[i].id, selectedStudents[i].name, selectedStudents[i].class); } fclose(fp); printf("保存成功!\n"); } int main() { Student students[STUDENT_COUNT]; Student selectedStudents[SELECT_COUNT]; read(students); select(students, selectedStudents); display(selectedStudents); write(selectedStudents); return 0; }