有如下结构体:
typedef struct Student{
char name[20];
int chinese;//语文成绩
int math;
int english;
int physical;
int chemical;
int biological;
}stu_t;
使用一张链表保存5个学生的信息,学生信息自己初始化
编写2个.c文件,save.c 和 load.c
save.c负责:
使用fprintf将3个学生的所有信息保存到文件中去
load.c负责:
使用fscanf读取文件中的3个学生的信息,将读取到的数据,保存到一个 stu_t arr[3]数组里面去,并输出arr数组中的内容
最终实现效果:
运行save.c,会将准备的学生信息写入文件中
运行load.c,会将文件中的所有学生信息输出到终端上
#ifndef STUDENT_LIST_H
#define STUDENT_LIST_H
#include <stdio.h>
#include <stdlib.h>
typedef struct Student {
char name[20];
int chinese;
int math;
int english;
int physical;
int chemical;
int biological;
} Student;
typedef struct Node {
Student stu;
struct Node *next;
} Node;
// 链表操作函数声明
Node* create_node(const Student *stu);
void free_list(Node *head);
void print_list(Node *head);
// 注意:save_students 和 load_students 将在各自的.c文件中实现
#endif
// save.c
#include "student_list.h"
Node* create_node(const Student *stu) {
Node *newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
perror("Failed to allocate memory for node");
exit(EXIT_FAILURE);
}
*newNode = (Node){*stu, NULL};
return newNode;
}
void save_students(const char *filename, Node *head) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
perror("Failed to open file");
return;
}
Node *current = head;
while (current != NULL) {
fprintf(fp, "%s %d %d %d %d %d %d\n", current->stu.name, current->stu.chinese, current->stu.math,
current->stu.english, current->stu.physical, current->stu.chemical, current->stu.biological);
current = current->next;
}
fclose(fp);
}
// 示例:在main中构建链表并保存
int main() {
// 构建链表(仅示例)
Student stu1 = {"Alice", 85, 90, 88, 92, 87, 91};
Student stu2 = {"Bob", 78, 85, 82, 88, 90, 87};
Student stu3 = {"Charlie", 92, 95, 90, 94, 91, 93};
Node *head = create_node(&stu1);
head->next = create_node(&stu2);
head->next->next = create_node(&stu3);
// 保存链表到文件
save_students("students.txt", head);
// 释放链表内存(重要!)
free_list(head);
return 0;
}
// 注意:free_list函数需要您自己实现
// load.c
#include "student_list.h"
#include <string.h>
// 辅助函数:在链表末尾添加一个新节点
void append_node(Node **head, const Student *stu) {
Node *newNode = create_node(stu);
if (*head == NULL) {
*head = newNode;
} else {
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 主函数:从文件中加载学生信息到链表
Node* load_students_to_list(const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("Failed to open file");
return NULL;
}
Node *head = NULL;
Student stu;
while (fscanf(fp, "%19s %d %d %d %d %d %d", stu.name, &stu.chinese, &stu.math,
&stu.english, &stu.physical, &stu.chemical, &stu.biological) == 7) {
append_node(&head, &stu);
}
fclose(fp);
return head;
}
// 示例main函数,用于演示加载链表并打印学生信息
int main() {
Node *head = load_students_to_list("students.txt");
if (head != NULL) {
Node *current = head;
while (current != NULL) {
printf("Student: %s, Chinese: %d, Math: %d, English: %d, Physical: %d, Chemical: %d, Biological: %d\n",
current->stu.name, current->stu.chinese, current->stu.math,
current->stu.english, current->stu.physical, current->stu.chemical, current->stu.biological);
current = current->next;
}
// 注意:在实际应用中,您应该释放链表占用的内存
// free_list(head); // 假设您已经实现了这个函数
}
return 0;
}
// 注意:create_node函数应该在student_list.h或某个.c文件中定义,这里假设它已经在其他地方定义了
// 如果还没有定义,请确保在student_list.h中声明它,并在某个.c文件中实现它