C语言超市商品管理系统带文件操作完整源码

在这里插入图片描述
需要源码请看昵称练好
下面展示一些 内联代码片

// A code block
var foo = 'bar';
// An highlighted block
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_PRODUCTS 100
#define FILENAME "inventory.txt"

typedef struct {
    int id;
    char name[50];
    char category[50];
    char production_date[20];
    int shelf_life;
    char last_stock_date[20];
    int stock;
    double price;
} Product;

typedef struct {
    Product products[MAX_PRODUCTS];
    int count;
    int totalStock; // 记录总库存量
} Inventory;

void initInventory(Inventory *inventory) {
    inventory->count = 0;
    inventory->totalStock = 0;
}
void saveInventoryToFile(Inventory *inventory) {
    FILE *file = fopen(FILENAME, "w");
    if (file == NULL) {
        printf("无法打开文件 %s\n", FILENAME);
        return;
    }

    fprintf(file, "%d\n", inventory->count);
    for (int i = 0; i < inventory->count; i++) {
        fprintf(file, "%d,%s,%s,%s,%d,%s,%d,%.2f\n",
                inventory->products[i].id,
                inventory->products[i].name,
                inventory->products[i].category,
                inventory->products[i].production_date,
                inventory->products[i].shelf_life,
                inventory->products[i].last_stock_date,
                inventory->products[i].stock,
                inventory->products[i].price);
    }

    fclose(file);
    printf("库存信息已保存到文件 %s\n", FILENAME);
}

void loadInventoryFromFile(Inventory *inventory) {
    FILE *file = fopen(FILENAME, "r");
    if (file == NULL) {
        printf("无法打开文件 %s\n", FILENAME);
        return;
    }

    int count;
    fscanf(file, "%d", &count);
    inventory->count = count;

    for (int i = 0; i < count; i++) {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐瑶万正源码,可堪头相,徐福费

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值