学生信息管理系统c语言从文件导入数据动态链表
时间: 2023-05-19 15:02:18 浏览: 121
非常感谢您的提问。以下是一个简单的学生信息管理系统的C语言代码,可以从文件导入数据到动态链表中:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
char name[20];
int id;
float score;
struct student *next;
} Student;
Student *create_student(char *name, int id, float score) {
Student *s = (Student *)malloc(sizeof(Student));
strcpy(s->name, name);
s->id = id;
s->score = score;
s->next = NULL;
return s;
}
void add_student(Student **head, Student *s) {
if (*head == NULL) {
*head = s;
} else {
Student *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = s;
}
}
void print_student(Student *s) {
printf("Name: %s, ID: %d, Score: %.2f\n", s->name, s->id, s->score);
}
void print_all_students(Student *head) {
Student *p = head;
while (p != NULL) {
print_student(p);
p = p->next;
}
}
void free_all_students(Student *head) {
Student *p = head;
while (p != NULL) {
Student *tmp = p;
p = p->next;
free(tmp);
}
}
int main() {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("Failed to open file!\n");
return 1;
}
Student *head = NULL;
char name[20];
int id;
float score;
while (fscanf(fp, "%s %d %f", name, &id, &score) == 3) {
Student *s = create_student(name, id, score);
add_student(&head, s);
}
fclose(fp);
print_all_students(head);
free_all_students(head);
return 0;
}
```
这个程序可以从名为"students.txt"的文件中读取学生的姓名、学号和成绩,然后将它们存储在动态链表中,并最终打印出所有学生的信息。如果您有任何问题,请随时问我。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)