48 lines
1.0 KiB
C
48 lines
1.0 KiB
C
|
// 按行读取score.txt文件的所有内容,打印成绩及格的学号和成绩。
|
|||
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <time.h>
|
|||
|
|
|||
|
typedef struct stu_s
|
|||
|
{
|
|||
|
int sid;
|
|||
|
char dh; // 逗号
|
|||
|
float score;
|
|||
|
|
|||
|
struct stu_s *next;
|
|||
|
} STU;
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
FILE *fp = fopen("score.txt", "r");
|
|||
|
|
|||
|
if (NULL == fp)
|
|||
|
{
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
// 计算文件大小
|
|||
|
fseek(fp, 0, SEEK_END);
|
|||
|
long file_size = ftell(fp);
|
|||
|
printf("score.txt 文件大小: %ld B\n", file_size);
|
|||
|
|
|||
|
// 重置光标位置
|
|||
|
fseek(fp, 0, SEEK_SET); // 等同于使用 rewind(fp);
|
|||
|
// 按照 STU 的结构存储数据,计算出以存放学生数量
|
|||
|
int stu_num = file_size / sizeof(STU);
|
|||
|
printf("学生数量: %d\n", stu_num);
|
|||
|
|
|||
|
printf("\n学号\t成绩\n");
|
|||
|
for (int i = 0; i < stu_num; i++)
|
|||
|
{
|
|||
|
STU *s = calloc(1, sizeof(STU));
|
|||
|
fread(s, sizeof(STU), 1, fp);
|
|||
|
if (s->score > 60)
|
|||
|
printf("%d\t%.2f\n", s->sid, s->score);
|
|||
|
free(s);
|
|||
|
}
|
|||
|
|
|||
|
fclose(fp);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|