C语言学习笔记七

任务四

简单文件读写

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp;
char ch;
int character = 0;
int line = 0;

fp = fopen("data4.txt", "r");
if (fp == NULL)
{
printf("fail to open\n");
return 1;
}

while ((ch = fgetc(fp)) != EOF)
{
if (ch == '\n')
line++;
if (ch != ' ' && ch != '\n' && ch != '\t')
character++;
}

if (ftell > 0 && ch != '\n')
line++;

printf("Total lines: %d\n", line);
printf("Total characters: %d\n", character);

fclose(fp);
return 0;
}

结果

任务五

文件综合应用 考试

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#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("Add %d students from file...\n", N);
read(stu, N);

printf("\nFiguring...\n");
cnt = process(stu, N, stu_pass);

printf("\nStudents passed the exam:\n");
output(stu, N); // 输出所有考生完整信息到屏幕
write(stu, N); // 输出考试通过的考生信息到文件

pass_rate = 1.0 * cnt / N;
printf("\nPass Rank: %.2f%%\n", pass_rate * 100);

return 0;
}

// 把所有考生完整信息输出到屏幕上
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void output(STU st[], int n)
{
int i;

printf("Number\tName\tObj_Score\tOpe_Score\tTotal_Score\tResult\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;
}

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 st[], int n)
{
int i, cnt = 0;
FILE *fp;

fp = fopen("list_pass.txt", "w");
if (fp == NULL)
{
printf("fail to open\n");
return;
}

for (i = 0; i < n; i++)
{
if (st[i].sum >= 60)
{
cnt++;
fprintf(fp, "%ld %s %.2f %.2f %.2f %s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
}
}

fclose(fp);

printf("%d students passed the exam\n", cnt);
}

// 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
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)
{
cnt++;
strcpy(st[i].result, "Passed");
strcpy(st_pass[cnt - 1].name, st[i].name);
st_pass[cnt - 1].id = st[i].id;
st_pass[cnt - 1].objective = st[i].objective;
st_pass[cnt - 1].subjective = st[i].subjective;
st_pass[cnt - 1].sum = st[i].sum;
strcpy(st_pass[cnt - 1].result, st[i].result);
}
else
{
strcpy(st[i].result, "Failed");
}
}

return cnt;
}

结果

任务六

文件综合应用 点名

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define M 100

typedef struct
{
int id;
char name[20];
char class[100];
} STU;

void shuffle(char *str[], int n)
{
for (int i = n - 1; i > 0; i--)
{
int j = rand() % (i + 1);
char *temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}

void sort(char *str[], int n)
{
STU stu[M];
for (int i = 0; i < n; i++)
{
if (sscanf(str[i], "%d\t%s\t%s", &stu[i].id, stu[i].name, stu[i].class) != 3)
{
printf("Error parsing string: %s\n", str[i]);
return;
}
}
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (stu[i].id > stu[j].id)
{
STU temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
snprintf(str[i], M, "%d\t%s\t%s", stu[i].id, stu[i].name, stu[i].class);
}
}

void save_to_file(char *str[], int n)
{
printf("# press \"c\" to set a custom file name\n");
printf("# press any other key to use default file name\n\n");
printf("choice: ");
char choice;
char filename[100];
FILE *fp;
scanf(" %c", &choice);

if (choice == 'c')
{
printf("enter a file name: ");
scanf("%99s", filename);
fp = fopen(filename, "w");
}
else
{
time_t t = time(NULL);
strftime(filename, sizeof(filename), "%Y-%m-%d_%H-%M-%S.txt", localtime(&t));
fp = fopen(filename, "w");
}

if (fp == NULL)
{
printf("\nfail to open\n");
return;
}

for (int i = 0; i < n; i++)
{
if (fprintf(fp, "%s\n", str[i]) < 0)
{
printf("Error\n");
break;
}
}

fclose(fp);
printf("\nfile saved as %s\n", filename);
}

int main()
{
FILE *fp = fopen("list.txt", "r");
if (fp == NULL)
{
printf("fail to open\n");
return 1;
}

char *data[M];
int cnt = 0;

while (cnt < M && !feof(fp))
{
data[cnt] = malloc(M * sizeof(char));
if (fgets(data[cnt], M, fp) != NULL)
{
data[cnt][strcspn(data[cnt], "\n")] = 0;
cnt++;
}
else
{
free(data[cnt]);
cnt--;
}
}
fclose(fp);

srand(time(NULL));
shuffle(data, cnt);

printf("---------\tRandom Picking\t---------\n\n");
char *picked[5];
for (int i = 0; i < 5; i++)
picked[i] = data[i];

sort(picked, 5);

for (int i = 0; i < 5; i++)
printf("%s\n", picked[i]);

printf("\n\n---------\tSave to File\t---------\n\n");

save_to_file(picked, 5);

for (int i = 0; i < cnt; i++)
free(data[i]);

return 0;
}

结果




__EOF__\_\_EOF\_\_



C语言学习笔记七
https://churk.top/2024/12/30/2024-12-30-01/
作者
Churk Ben
发布于
2024年12月30日
许可协议