43 lines
931 B
C
43 lines
931 B
C
// 随机生成10位学生的成绩并写入到score.txt文件中
|
|
// 【要求】随机生成学号和成绩,每一行存储一位学生的成绩,学号与成绩之间用 ","逗号分隔。
|
|
// 【提示】成绩范围[0, 100]
|
|
#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", "w");
|
|
srand(time(NULL));
|
|
// float scorex = rand() % 101;
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
if (NULL == fp)
|
|
{
|
|
perror("fopen");
|
|
return 1;
|
|
}
|
|
STU *stu = malloc(sizeof(STU));
|
|
|
|
stu->sid = i + 1;
|
|
stu->dh = ','; // 间隔的逗号
|
|
stu->score = rand() % 101;
|
|
fwrite(stu, sizeof(STU), 1, fp);
|
|
// fputc('\n', fp); // 添加换行
|
|
|
|
free(stu);
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
} |