Linux:班级有5名学生,每名学生的信息如下:姓名,年龄,语文成绩,数学成绩,外语成绩。编程实现如下功能: 1、通过键盘给这5名学生赋值,并通过调用函数fwrite或write,保存信息到文件student.txt中。 2、从文件student.txt中通过调用fread或read,读取这5名学生的信息,并显示在屏幕上。
时间: 2024-04-08 20:34:08 浏览: 19
以下是使用C语言实现的代码,可以完成你提出的要求:
```c
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int age;
float chineseScore;
float mathScore;
float englishScore;
};
void inputStudentInfo(struct Student *students, int numStudents) {
for (int i = 0; i < numStudents; i++) {
printf("请输入第%d位学生的信息:\n", i+1);
printf("姓名:");
scanf("%s", students[i].name);
printf("年龄:");
scanf("%d", &students[i].age);
printf("语文成绩:");
scanf("%f", &students[i].chineseScore);
printf("数学成绩:");
scanf("%f", &students[i].mathScore);
printf("外语成绩:");
scanf("%f", &students[i].englishScore);
}
}
void saveStudentInfo(struct Student *students, int numStudents) {
FILE *file = fopen("student.txt", "wb");
if (file == NULL) {
printf("无法打开文件\n");
return;
}
fwrite(students, sizeof(struct Student), numStudents, file);
fclose(file);
}
void loadStudentInfo(struct Student *students, int numStudents) {
FILE *file = fopen("student.txt", "rb");
if (file == NULL) {
printf("无法打开文件\n");
return;
}
fread(students, sizeof(struct Student), numStudents, file);
fclose(file);
}
void displayStudentInfo(struct Student *students, int numStudents) {
for (int i = 0; i < numStudents; i++) {
printf("第%d位学生的信息:\n", i+1);
printf("姓名:%s\n", students[i].name);
printf("年龄:%d\n", students[i].age);
printf("语文成绩:%.2f\n", students[i].chineseScore);
printf("数学成绩:%.2f\n", students[i].mathScore);
printf("外语成绩:%.2f\n", students[i].englishScore);
}
}
int main() {
int numStudents = 5;
struct Student students[numStudents];
inputStudentInfo(students, numStudents);
saveStudentInfo(students, numStudents);
loadStudentInfo(students, numStudents);
displayStudentInfo(students, numStudents);
return 0;
}
```
使用上述代码,你可以通过键盘输入5名学生的信息,并将信息保存到名为student.txt的文件中。然后,你可以从该文件中读取学生的信息,并将其显示在屏幕上。请确保在运行代码之前已经创建了名为student.txt的文件,并且具有写入权限。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)
![](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)