10101010

void loadUsersFromFile() {

    FILE* file = fopen("users.csv", "r");

    if (file == NULL) {

        printf("Failed to open file.\n");

        return;

    }

    char line[200];

    while (fgets(line, sizeof(line), file)) {

        // 去除行尾的换行符

        if (line[strlen(line) - 1] == '\n') {

            line[strlen(line) - 1] = '\0';

        }

        User* newUser = (User*)malloc(sizeof(User));

        // 使用逗号分隔行中的字段

        char* token = strtok(line, ",");

        newUser->id = atoi(token);

        token = strtok(NULL, ",");

        strcpy(newUser->username, token);

        token = strtok(NULL, ",");

        strcpy(newUser->password, token);

        token = strtok(NULL, ",");

        strcpy(newUser->phone, token);

        token = strtok(NULL, ",");

        strcpy(newUser->email, token);

        // 插入节点到链表中

        if (head == NULL || newUser->id < head->id) {

            newUser->next = head;

            head = newUser;

        } else {

            User* current = head;

            while (current->next != NULL && current->next->id < newUser->id) {

                current = current->next;

            }

            newUser->next = current->next;

            current->next = newUser;

        }

    }

    fclose(file);

}

// 将链表中的用户信息写入.csv文件

void saveUsersToFile() {

    FILE* file = fopen("users.csv", "w");

    if (file == NULL) {

        printf("Failed to open file.\n");

        return;

    }

    User* current = head;

    while (current != NULL) {

        fprintf(file, "%d,%s,%s,%s,%s\n", current->id, current->username, current->password, current->phone, current->email);

        current = current->next;

    }

    fclose(file);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值