学习嵌入式的第21天

有如下结构体:
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文件中实现它

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值