C语言实战2

C语言实战案例:文件操作与数据结构

案例目标:

实现一个学生信息管理系统,包含文件读写、链表操作和基本增删改查功能。

文件读写与链表结合

定义学生结构体并创建链表:

复制代码
typedef struct Student {
    int id;
    char name[50];
    float score;
    struct Student *next;
} Student;

文件写入函数示例:

复制代码
void saveToFile(Student *head, const char *filename) {
    FILE *file = fopen(filename, "w");
    if (!file) return;
    
    Student *current = head;
    while (current != NULL) {
        fprintf(file, "%d %s %.2f\n", current->id, current->name, current->score);
        current = current->next;
    }
    fclose(file);
}

动态内存管理

从文件加载数据到链表:

复制代码
Student* loadFromFile(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) return NULL;

    Student *head = NULL, *tail = NULL;
    while (!feof(file)) {
        Student *newNode = (Student*)malloc(sizeof(Student));
        if (fscanf(file, "%d %s %f", &newNode->id, newNode->name, &newNode->score) == 3) {
            newNode->next = NULL;
            if (!head) head = newNode;
            else tail->next = newNode;
            tail = newNode;
        } else {
            free(newNode);
            break;
        }
    }
    fclose(file);
    return head;
}

实用功能实现

链表插入与删除操作:

复制代码
void insertStudent(Student **head, int id, const char *name, float score) {
    Student *newNode = (Student*)malloc(sizeof(Student));
    newNode->id = id;
    strcpy(newNode->name, name);
    newNode->score = score;
    newNode->next = *head;
    *head = newNode;
}

void deleteStudent(Student **head, int id) {
    Student *temp = *head, *prev = NULL;
    while (temp != NULL && temp->id != id) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return;
    if (prev == NULL) *head = temp->next;
    else prev->next = temp->next;
    free(temp);
}

高级应用示例

排序功能实现(冒泡排序):

复制代码
void sortByScore(Student **head) {
    if (!*head || !(*head)->next) return;
    
    int swapped;
    Student *ptr1;
    Student *lptr = NULL;
    
    do {
        swapped = 0;
        ptr1 = *head;
        while (ptr1->next != lptr) {
            if (ptr1->score < ptr1->next->score) {
                // 交换数据域
                Student temp = *ptr1;
                ptr1->id = ptr1->next->id;
                strcpy(ptr1->name, ptr1->next->name);
                ptr1->score = ptr1->next->score;
                
                ptr1->next->id = temp.id;
                strcpy(ptr1->next->name, temp.name);
                ptr1->next->score = temp.score;
                
                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    } while (swapped);
}

错误处理增强

增加文件操作安全检查:

复制代码
Student* safeLoad(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Error opening file");
        return NULL;
    }

    // ...(其余加载逻辑)
    
    if (ferror(file)) {
        perror("Error reading file");
        clearerr(file);
        fclose(file);
        return NULL;
    }
    fclose(file);
    return head;
}

该案例完整实现了文件持久化存储、动态内存管理、链表操作等核心功能,可作为C语言中级练习的典型范例。实际开发时可进一步扩展搜索功能、界面交互等模块。

相关推荐
寻星探路3 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly2024065 小时前
Bootstrap 警告框
开发语言
2601_949146535 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧5 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX6 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb01036 小时前
C++课后习题训练记录Day98
开发语言·c++
盟接之桥6 小时前
盟接之桥说制造:引流品 × 利润品,全球电商平台高效产品组合策略(供讨论)
大数据·linux·服务器·网络·人工智能·制造
猫头虎6 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
YUJIANYUE7 小时前
PHP纹路验证码
开发语言·php
会员源码网7 小时前
理财源码开发:单语言深耕还是多语言融合?看完这篇不踩坑
网络·个人开发