用c语言编写一个程序实现以下3个功能:(1)从文本文件导入学生的数据;(2)显示给定序号学生信息;(3)输出学生成绩表。
时间: 2023-02-06 12:09:23 浏览: 181
以下是一个示例程序,实现了上述三个功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最大学生数
// 学生信息结构体
typedef struct {
int id; // 学号
char name[32]; // 姓名
int score; // 成绩
} Student;
int import_students_from_file(Student *students, const char *filename);
void display_student_info(Student *students, int id);
void print_grade_report(Student *students, int num_students);
int main(int argc, char *argv[]) {
Student students[MAX_STUDENTS];
int num_students;
// 从文件导入学生数据
num_students = import_students_from_file(students, "students.txt");
if (num_students < 0) {
fprintf(stderr, "Error importing students from file.\n");
return 1;
}
// 显示给定序号的学生信息
display_student_info(students, 12345);
// 输出学生成绩表
print_grade_report(students, num_students);
return 0;
}
// 从文件导入学生数据
int import_students_from_file(Student *students, const char *filename) {
FILE *fp;
int i;
fp = fopen(filename, "r");
if (!fp) {
return -1; // 文件打开失败
}
i = 0;
while (fscanf(fp, "%d,%[^,],%d", &students[i].id, students[i].name, &students[i].score) == 3) {
i++;
}
fclose(fp);
return i; // 返回导入的学生数
}
// 显示给定序号的学生信息
void display_student_info(Student *students, int id) {
int i;
for (i = 0; i < MAX_STUDENTS; i++) {
if (students[i].id == id) {
printf("学号: %d\n", students[i].id);
printf("姓名: %s\n", students[i].name);
printf("成绩: %d\n", students[i].score);
return;
}
}
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](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)
![](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)