建立一个数组将学生数据存入数组。 数据为: { {"2020B200301","刘滨","男",18,{2020,9,20},70,85}, {"2020B200302","阚美娟","女",17,{2020,9,20},87,95}, {"2020B200303","胡大勇","男",17,{2020,9,20},69,80}, {"2020B200305","黎丽","女",18,{2020,9,20},68,88}, {"2020B200309","金鑫","男",19,{2019,9,1},90,85} } 将这些数据写人磁盘保存起来。
时间: 2023-07-19 07:57:44 浏览: 38
好的,这里是使用 C 语言的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char id[12];
char name[20];
char gender[6];
int age;
int birthday[3];
int score1;
int score2;
} Student;
int main() {
// 定义并初始化学生数据
Student students[] = {
{"2020B200301", "刘滨", "男", 18, {2020, 9, 20}, 70, 85},
{"2020B200302", "阚美娟", "女", 17, {2020, 9, 20}, 87, 95},
{"2020B200303", "胡大勇", "男", 17, {2020, 9, 20}, 69, 80},
{"2020B200305", "黎丽", "女", 18, {2020, 9, 20}, 68, 88},
{"2020B200309", "金鑫", "男", 19, {2019, 9, 1}, 90, 85}
};
int num_students = sizeof(students) / sizeof(Student);
// 将学生数据写入文件
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("Failed to open file.\n");
return 1;
}
for (int i = 0; i < num_students; i++) {
Student s = students[i];
fprintf(fp, "%s,%s,%s,%d,%d-%d-%d,%d,%d\n", s.id, s.name, s.gender, s.age,
s.birthday[0], s.birthday[1], s.birthday[2], s.score1, s.score2);
}
fclose(fp);
printf("Student data saved to file.\n");
return 0;
}
```
这段代码定义了一个 `Student` 的结构体,存储了每个学生的数据。然后将学生数据写入到名为 `students.txt` 的文件中。每行数据使用逗号分隔,第一列是学号,第二列是姓名,第三列是性别,第四列是年龄,第五列是生日(格式为年-月-日),第六列是第一门课的成绩,第七列是第二门课的成绩。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![mp4](https://img-home.csdnimg.cn/images/20210720083504.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)