C语言链表库存更新功能求助:字符串匹配与文件更新异常
基于链表的库存管理程序问题修复方案
现存问题及对应修复
1. 字符串输入匹配失败
- 问题根源:更新函数仅对比当前
curr节点,未遍历整个链表查找匹配的物资编码;输入验证循环错误使用逗号表达式,未正确调用strcmp判断减号输入;code数组长度(10)小于结构体中supplyCode长度(20),可能导致输入截断。 - 修复措施:
- 遍历整个链表,逐个对比节点的
supplyCode与用户输入; - 修正输入验证条件,正确使用
strcmp判断+/-; - 将
code数组长度调整为20,与结构体定义一致。
- 遍历整个链表,逐个对比节点的
2. 库存数量增减运算失效
- 问题根源:仅实现了数量增加逻辑,未处理减少情况;以追加模式打开文件,仅写入修改后的数量,未更新整个库存记录;未确保操作的是匹配到的目标节点。
- 修复措施:
- 添加数量减少的分支逻辑;
- 以写入模式("w")打开文件,重新写入整个链表的更新后数据;
- 找到匹配节点后,针对该节点执行数量运算。
3. 字符串未以零结尾警告
- 问题根源:局部字符串数组未初始化,
gets_s在极端情况下可能无法正确终止字符串。 - 修复措施:初始化局部字符串数组为全0;使用
scanf_s时指定输入长度,确保字符串正确终止。
修改后的完整代码
链表结构体定义
struct donations { char supplyName[100], supplyCode[20], donator[150]; int no_ofShipment; float quantityReceived; struct donations* ptr; } *start, *curr, *temp;
库存创建主函数(优化版)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> void details(); void update_DonationQuantity(); int main() { int i; FILE *fp; start = curr = NULL; if (start == NULL) { for (i = 0; i < 5; i++) { curr = (struct donations *)malloc(sizeof(struct donations)); memset(curr, 0, sizeof(struct donations)); details(); if (i == 0) start = temp = curr; else { temp->ptr = curr; temp = curr; } } temp->ptr = NULL; temp = start; printf("\n\t Inventory Created Successfully!"); printf("\n\t Recorded to the end of the list!"); } if (fopen_s(&fp, "Donation.txt", "w") != 0) { printf("\nError opening file!"); return 1; } curr = start; while (curr) { fprintf(fp, "%-30s", curr->supplyName); fprintf(fp, "%-30s", curr->supplyCode); fprintf(fp, "%-30s", curr->donator); fprintf(fp, "%10d", curr->no_ofShipment); fprintf(fp, "\t%.1f\n", curr->quantityReceived); curr = curr->ptr; } printf("\n\n\t File (records) has been created!"); fclose(fp); printf("\n\n\n\tDisplay Inventory"); temp = start; while (temp) { printf("\n\n\n\t Name of Supply: %-30s", temp->supplyName); printf("Supply Code: %-30s", temp->supplyCode); printf("Donator: %-30s", temp->donator); printf("No. of Shipment: %10d", temp->no_ofShipment); printf("\tQuantity Received: %.1f", temp->quantityReceived); temp = temp->ptr; } update_DonationQuantity(); return 0; } void details() { system("cls"); printf("\tInventory Creation\t"); printf("\n\n\tEnter Name of Supply: "); scanf_s("%99s", curr->supplyName, (unsigned)_countof(curr->supplyName)); while (getchar() != '\n'); printf("\n\tEnter Supply Code: "); scanf_s("%19s", curr->supplyCode, (unsigned)_countof(curr->supplyCode)); while (getchar() != '\n'); printf("\n\tEnter Donator: "); scanf_s("%149s", curr->donator, (unsigned)_countof(curr->donator)); while (getchar() != '\n'); printf("\n\tEnter No. of Shipment: "); scanf_s("%d", &curr->no_ofShipment); while (getchar() != '\n'); printf("\n\tEnter Quantity Received: "); scanf_s("%f", &curr->quantityReceived); while (getchar() != '\n'); }
修复后的更新功能函数
void update_DonationQuantity() { char code[20] = {0}; char update[5] = {0}; float quantity; FILE *fp; struct donations *target = NULL; printf("\n\tUpdate Donation Quantity\t\n"); printf("\n\n\tSelect donation (CT/HS/FM/SM/OM): "); scanf_s("%19s", code, (unsigned)_countof(code)); while (getchar() != '\n'); target = start; while (target != NULL) { if (strcmp(code, target->supplyCode) == 0) { break; } target = target->ptr; } if (target == NULL) { printf("\n\tNo matching supply code found!"); return; } printf("\n\tReceived/Distributed item (+/-): "); scanf_s("%4s", update, (unsigned)_countof(update)); while (getchar() != '\n'); while (strcmp(update, "+") != 0 && strcmp(update, "-") != 0) { printf("\n\tWrong input, Try Again"); printf("\n\tReceived/Distributed item (+/-): "); scanf_s("%4s", update, (unsigned)_countof(update)); while (getchar() != '\n'); } printf("\n\t Enter amount: "); scanf_s("%f", &quantity); while (getchar() != '\n'); if (strcmp(update, "+") == 0) { target->quantityReceived += quantity; } else { if (target->quantityReceived >= quantity) { target->quantityReceived -= quantity; } else { printf("\n\tInsufficient quantity!"); return; } } if (fopen_s(&fp, "Donation.txt", "w") != 0) { printf("\nError opening file!"); return; } curr = start; while (curr) { fprintf(fp, "%-30s", curr->supplyName); fprintf(fp, "%-30s", curr->supplyCode); fprintf(fp, "%-30s", curr->donator); fprintf(fp, "%10d", curr->no_ofShipment); fprintf(fp, "\t%.1f\n", curr->quantityReceived); curr = curr->ptr; } printf("\n\t Inventory updated successfully!"); fclose(fp); printf("\n\n\n\tUpdated Inventory"); temp = start; while (temp) { printf("\n\n\n\t Name of Supply: %-30s", temp->supplyName); printf("Supply Code: %-30s", temp->supplyCode); printf("Donator: %-30s", temp->donator); printf("No. of Shipment: %10d", temp->no_ofShipment); printf("\tQuantity Received: %.1f", temp->quantityReceived); temp = temp->ptr; } }
内容的提问来源于stack exchange,提问作者Beginner
相关产品推荐
相关产品推荐

