实验7 ⽂件应⽤编程
实验结论
实验任务1-3无需写入博客文档
4.实验任务4
task4.c源代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int line(FILE* fp);
int number(FILE* fp);
int main()
{
int linex, num;
FILE* fp;
fp = fopen("data4.txt", "r");
if (!fp) {
printf("fail to open file to read\n");
return -1;
}
linex = line(fp);
num = number(fp);
fclose(fp);
printf("data4.txt统计结果:\n行数: %-5d\n字符数(不计空白符):%-5d", linex, num);
return 0;
}
int line(FILE* fp) {
int count = 0;
rewind(fp);
while (!feof(fp)) {
if (fgetc(fp) == '\n')
count++;
}
count += 1;
return count;
}
int number(FILE* fp) {
int count = 0;
char flag;
rewind(fp);
while (!feof(fp)) {
flag = fgetc(fp);
if ( flag!= '\n' && flag != ' ' && flag != '\t'&& flag != EOF)
count++;
}
return count;
}
运行结果截图

5.实验任务5
task5.c源代码
#define _CRT_SECURE_NO_WARNINGS 1
#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, N); // 输出考试通过的考生信息到文件
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\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;
}
while (!feof(fin)) {
for (i = 0; i < n; i++)
fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
}
fclose(fin);
}
// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write(STU s[], int n) {
int i;
FILE* fp;
fp = fopen("list_pass.txt", "w");
fprintf(fp, "%-10s %-8s %-10s %-10s %-10s %-8s\n", "准考证号", "姓名", "客观题得分", "操作题得分", "总分", "结果");
for (i = 0; i < n; i++) {
if (s[i].sum >= 60)
fprintf(fp, "%-10ld %-8s %-10.2f %-10.2f %-10.2f %-8s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
}
fclose(fp);
}
// 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
int process(STU st[], int n, STU st_pass[]) {
int i, j;
for (i = 0; i < n; ++i) {
st[i].sum = st[i].objective + st[i].subjective;
}
for (i = 0, j = 0; i < n; ++i) {
if (st[i].sum >= 60) {
strcpy(st[i].result, "通过");
st_pass[j] = st[i];
j++;
}
else {
strcpy(st[i].result, "不通过");
}
}
return j;
}
运行结果截图


6.实验任务6
必做部分
- task6.c源代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define N 5//抽选人数
#define M 80//班级总人数
#define MAX 100
typedef struct
{
long id;//学号
char name[20];//姓名
char classname[40]; //班级
} STU;
void random(STU temp[], STU a[], int n, int m);//从总共m个人的temp[]中随机选取n个人并存入a[]中
void read(FILE* fp, STU temp[], int n);//从fp所指的文件里读取n名学生信息并保存到temp[]中
void output(STU a[], int n);//向屏幕上输出中奖名单
void write(STU a[], int n, char filename[]);//将名单保存至文件中
int main() {
STU a[N], temp[M];
FILE* fp;
fp = fopen("list.txt", "r");
if (!fp) {
printf("Fail to open\n");
return;
}
read(fp, temp, M);
fclose(fp);
random(temp, a, N, M);
output(a, N);
printf("------------保存到文件------------\n");
char filename[20];
printf("输入文件名:");
scanf("%s", filename);
write(a, N, filename);
fp = fopen(filename, "r");
if (!fp)
printf("保存失败!\n");
else
printf("保存成功!\n");
fclose(fp);
return 0;
}
void random(STU temp[], STU a[], int n, int m) {
int i, num;
int flag[MAX] = { 0 };
srand((unsigned)time(NULL));
for (i = 0; i < n;) {
num = rand() % m;
if (!flag[num]) {
a[i] = temp[num];
flag[num] = 1;
i++;
}
}
}
void read(FILE* fp, STU temp[], int n) {
int i;
while (!feof(fp)) {
for (i = 0; i < n; ++i)
fscanf(fp, "%ld %s %s", &temp[i].id, temp[i].name, temp[i].classname);
}
}
void output(STU a[], int n) {
int i;
printf("------------中奖名单------------\n");
for (i = 0; i < n; ++i) {
printf("%-20d%-10s%s\n", a[i].id, a[i].name, a[i].classname);
}
}
void write(STU a[], int n, char filename[]) {
FILE* fp;
fp = fopen(filename, "w");
int i;
for (i = 0; i < n; ++i) {
fprintf(fp, "%-20d%-15s%s\n", a[i].id, a[i].name, a[i].classname);
}
fclose(fp);
}
- 运行测试截图
![image]()

选做部分*
- task6_1.c源代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define N 5//抽选人数
#define M 80//班级总人数
#define MAX 100
typedef struct
{
long id;//学号
char name[20];//姓名
char classname[40]; //班级
} STU;
void random(STU temp[], STU a[], int n, int m);//从总共m个人的temp[]中随机选取n个人并存入a[]中
void sorting(STU a[],int n);//对a[]中的n个人进行排序,结果覆盖在a[]中
void read(FILE* fp, STU temp[], int n);//从fp所指的文件里读取n名学生信息并保存到temp[]中
void output(STU a[], int n, char buffer[]);//向屏幕上输出当天(buffer[])的中奖名单
void write(STU a[], int n, char buffer[], char filename[]);//将当天(buffer[])的名单保存到名为filename[]的文件中
int main() {
STU a[N], temp[M];
time_t rawtime;
struct tm* info;
char buffer[80];
time(&rawtime);
info = localtime(&rawtime);
strftime(buffer, 80, "%Y%m%d", info);
char filename[20];
sprintf(filename, "%s.txt", buffer);
FILE* fp;
fp = fopen("list.txt", "r");
if (!fp) {
printf("Fail to open\n");
return;
}
read(fp,temp, M);
fclose(fp);
random(temp, a, N, M);
sorting(a, N);
output(a, N, buffer);
write(a, N, buffer, filename);
fp = fopen(filename, "r");
if (!fp)
printf("文件保存失败!\n");
else
printf("文件保存成功!\n");
fclose(fp);
return 0;
}
void random(STU temp[], STU a[], int n, int m) {
int i,num;
int flag[MAX] = { 0 };
srand((unsigned)time(NULL));
for (i = 0; i < n;) {
num = rand() % m;
if (!flag[num]) {
a[i] = temp[num];
flag[num] = 1;
i++;
}
}
}
void sorting(STU a[], int n) {
int i, j;
long t;
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (a[j].id > a[j + 1].id) {
t = a[j].id;
a[j].id = a[j + 1].id;
a[j + 1].id = t;
}
}
}
}
void read(FILE* fp, STU temp[], int n) {
int i;
while (!feof(fp)) {
for (i = 0; i < n; ++i)
fscanf(fp, "%ld %s %s", &temp[i].id, temp[i].name, temp[i].classname);
}
}
void output(STU a[], int n, char buffer[]) {
int i;
printf("------------%s中奖名单------------\n", buffer);
for (i = 0; i < n; ++i) {
printf("%-20d%-10s%s\n", a[i].id, a[i].name, a[i].classname);
}
}
void write(STU a[], int n, char buffer[], char filename[]) {
FILE* fp;
fp = fopen(filename, "w");
int i;
for (i = 0; i < n; ++i) {
fprintf(fp, "%-20d%-15s%s\n", a[i].id, a[i].name, a[i].classname);
}
fclose(fp);
}
- 运行测试截图
![image]()




浙公网安备 33010602011771号