创建了一个设备信息管理系统,用的链表,接下来怎么实现“保存”功能?即存到文件里。


#include
#include//调用malloc函数,调用system pause
#include
#include//为了调用字符串比较函数strcmp,从而进行按照品名查找

//设备信息
typedef struct _Equipment
{
    int equNum;//设备编号
    char name[20];//名称
    int price;//价格
    int number;//数量
}Equipment;//定义结构体名称

//结点信息
typedef struct _Node
{
    Equipment equipment;//结点里面首先要保存设备信息,这是一个结构体的类型
    struct _Node* next;//保存下一个结点的地址
}Node;
//↑↑↑这样两个结构体类型就定义好了↑↑↑

void welcome();                  //对菜单函数进行声明
void inputEquipment(Node* head); //对增加设备函数进行声明
void printEquipment(Node* head); //对显示设备函数进行声明
void findEquipment(Node* head);  //对查找设备函数进行声明
void deleteEquipment(Node* head);//对删除设备函数进行声明


int main()//主函数
{
    //创建头节点
    Node* head = malloc(sizeof(Node));
    head->next = NULL;
    
    while (1)
    {
        welcome();
        char c = getch();
        switch (c)     //使用switch进行选择
        {
        case'0':  //退出程序
            printf("已退出程序!!!\n");
            return 0;
            break;
        case'1':
            inputEquipment(head);//增加设备 
            break;
        case'2':
            deleteEquipment(head);//删除一定数量的设备
            break;
        case'3':
            findEquipment(head);//查找设备
            break;
        case'4':
            printEquipment(head);//显示所有设备
            break;
        default:
            printf("\a\a");  //出现错误的提示音
            printf("输入错误!!!请重新输入!!!(0/1/2/3/4)\n");
            break;
        }
    }
    return 0;
}
//↑↑↑主函数↑↑↑

void welcome()
{
    printf("                                          ********设备管理系统********\n");
    printf("                                          ***     0.退出系统       ***\n");
    printf("                                          ***     1.增加设备       ***\n");
    printf("                                          ***     2.删除设备       ***\n");
    printf("                                          ***     3.查找设备       ***\n");
    printf("                                          ***     4.显示所有设备    ***\n");
    printf("                                          ****************************\n");
    printf("********************设定规则:有3种设备:TV DVD TVwithDVD;其中TV与DVD都为500元,TVwithDVD为800元***********************\n");
    printf("请选择>");
}         
//↑↑↑菜单函数,打印菜单↑↑↑

void inputEquipment(Node*head)
{
    Node*fresh=malloc(sizeof(Node));//申请一块空间,让用户输入的信息存储到这个空间里
    fresh->next = NULL;//这个新的节点后面是没有数据的,即指向空
    printf("请输入设备编号,名称,价格,数量");
    scanf("%d%s%d%d", &fresh->equipment.equNum, fresh->equipment.name, &fresh->equipment.price, &fresh->equipment.number);//输入的信息就存在刚才申请的空间里
    printf("是否保存?y/n\n");
    char c = getch();
    switch (c)
    {
    case'n':
    case'N':
        return 0;
        break;
    case'y':
    case'Y':
        Node * move = head;
        while (move->next != NULL)
        {
            move = move->next;
        }
        //将设备信息插入到尾部
        move->next = fresh;
        printf("已保存!!!\n");
        break;
    }
    system("pause");
    system("cls");
}
//↑↑↑输入信息函数,即增加设备,其中增加设备时提示是否保存(y/n)↑↑↑

void printEquipment(Node* head)
{
    Node* move = head->next;
    while (move != NULL)
    {
        printf("编号:%d 名称:%s 价格:%d 数量:%d\n", move->equipment.equNum, move->equipment.name, move->equipment.price, move->equipment.number);
        move = move->next;
    }
    system("pause");
    system("cls");
    
}
//↑↑↑打印设备信息(显示所有设备信息)↑↑↑

void findEquipment(Node* head)
{
    printf("请输入要查找的设备品名:");
    char name[20];
    scanf_s("%s", name,20);
     Node*move = head->next;
     while (move!=NULL)
     {
         if (strcmp(name, move->equipment.name)==0)
         {
             printf("编号:%d 姓名:%s 价格:%d 数量:%d\n", move->equipment.equNum, move->equipment.name, move->equipment.price, move->equipment.number);
            system("pause");
            system("cls");
             return;
         }
         move = move->next;
     }
     printf("未能找到设备信息!\n");
     system("pause");
     system("cls");
}
//↑↑↑按照品名(TV/DVD/TVwithDVD)查找该种设备的信息↑↑↑

void deleteEquipment(Node* head)
{
    char name[20];
    int i;
    printf("请输入要删除的设备品名:");
    scanf_s("%s", name, 20);
    printf("请输入要删除的数量:");
    scanf("%d", &i);
    Node* move = head->next;
    while (move != NULL)
    {
        if (strcmp(name, move->equipment.name) == 0)
        {
            move->equipment.number -= i;
            system("pause");
            system("cls");
        }
        move = move->next;
    }
    printf("未能找到设备信息!\n");
    system("pause");
    system("cls");
}
//↑↑↑按照品名去出库(删除)一定数量的设备↑↑↑

遍历节点数据,fopen打开文件,fprintf写入数据就行了

新增两个函数:void writefile(Node* head); 写文件函数 ,void readfile(Node* head); 读文件函数,其他问题的修改完善,见注释“修改”处,供参考:

#include<stdio.h>
#include<stdlib.h>//调用malloc函数,调用system pause
#include<conio.h>
#include<string.h>//为了调用字符串比较函数strcmp,从而进行按照品名查找

//设备信息
typedef struct _Equipment
{
    int equNum;//设备编号
    char name[20];//名称
    int price;//价格
    int number;//数量
}Equipment;//定义结构体名称

//结点信息
typedef struct _Node
{
    Equipment equipment;//结点里面首先要保存设备信息,这是一个结构体的类型
    struct _Node* next;//保存下一个结点的地址
}Node;
//↑↑↑这样两个结构体类型就定义好了↑↑↑

void welcome();                  //对菜单函数进行声明
void inputEquipment(Node* head); //对增加设备函数进行声明
void printEquipment(Node* head); //对显示设备函数进行声明
void findEquipment(Node* head);  //对查找设备函数进行声明
void deleteEquipment(Node* head);//对删除设备函数进行声明

void writefile(Node* head);    // 修改   写文件函数
void readfile(Node* head);     // 修改   读文件函数  

int main()//主函数
{
    //创建头节点
    Node* head = (Node*)malloc(sizeof(Node));  //Node* head = malloc(sizeof(Node)); 修改
    head->next = NULL;
    readfile(head);      // 修改
    while (1)
    {
        welcome();
        char c = getch();
        switch (c)     //使用switch进行选择
        {
        case'0':  //退出程序
            writefile(head);     // 修改
            printf("已退出程序!!!\n");
            return 0;
            break;
        case'1':
            inputEquipment(head);//增加设备 
            break;
        case'2':
            deleteEquipment(head);//删除一定数量的设备
            break;
        case'3':
            findEquipment(head);//查找设备
            break;
        case'4':
            printEquipment(head);//显示所有设备
            break;
        default:
            printf("\a\a");  //出现错误的提示音
            printf("输入错误!!!请重新输入!!!(0/1/2/3/4)\n");
            break;
        }
    }
    return 0;
}
//↑↑↑主函数↑↑↑

void welcome()
{
    printf("                                          ********设备管理系统********\n");
    printf("                                          ***     0.退出系统       ***\n");
    printf("                                          ***     1.增加设备       ***\n");
    printf("                                          ***     2.删除设备       ***\n");
    printf("                                          ***     3.查找设备       ***\n");
    printf("                                          ***     4.显示所有设备    ***\n");
    printf("                                          ****************************\n");
    printf("********************设定规则:有3种设备:TV DVD TVwithDVD;其中TV与DVD都为500元,TVwithDVD为800元***********************\n");
    printf("请选择>");
}
//↑↑↑菜单函数,打印菜单↑↑↑

void inputEquipment(Node* head)
{
    Node* fresh = (Node*)malloc(sizeof(Node));   //修改
    //Node* fresh = malloc(sizeof(Node));//申请一块空间,让用户输入的信息存储到这个空间里
    fresh->next = NULL;//这个新的节点后面是没有数据的,即指向空
    printf("请输入设备编号,名称,价格,数量");
    scanf("%d%s%d%d", &fresh->equipment.equNum, fresh->equipment.name, 
                    &fresh->equipment.price, &fresh->equipment.number);//输入的信息就存在刚才申请的空间里
    printf("是否保存?y/n\n");
    char c = getch();
    switch (c)
    {
    case'n':
    case'N':
        free(fresh);         //修改
        return;  //return 0;   修改
        break;
    case'y':
    case'Y':
        Node * move = head;
        while (move->next != NULL)
        {
            move = move->next;
        }
        //将设备信息插入到尾部
        move->next = fresh;
        printf("已保存!!!\n");
        break;
    }
    system("pause");
    system("cls");
}
//↑↑↑输入信息函数,即增加设备,其中增加设备时提示是否保存(y/n)↑↑↑

void printEquipment(Node* head)
{
    Node* move = head->next;
    while (move != NULL)
    {
        printf("编号:%d 名称:%s 价格:%d 数量:%d\n", move->equipment.equNum, 
            move->equipment.name, move->equipment.price, move->equipment.number);
        move = move->next;
    }
    system("pause");
    system("cls");

}
//↑↑↑打印设备信息(显示所有设备信息)↑↑↑

void findEquipment(Node* head)
{
    printf("请输入要查找的设备品名:");
    char name[20];
    scanf_s("%s", name, 20);
    Node* move = head->next;
    while (move != NULL)
    {
        if (strcmp(name, move->equipment.name) == 0)
        {
            printf("编号:%d 姓名:%s 价格:%d 数量:%d\n", move->equipment.equNum, 
                move->equipment.name, move->equipment.price, move->equipment.number);
            system("pause");
            system("cls");
            return;
        }
        move = move->next;
    }
    printf("未能找到设备信息!\n");
    system("pause");
    system("cls");
}
//↑↑↑按照品名(TV/DVD/TVwithDVD)查找该种设备的信息↑↑↑

void deleteEquipment(Node* head)
{
    char name[20];
    int i;
    printf("请输入要删除的设备品名:");
    scanf_s("%s", name, 20);
    printf("请输入要删除的数量:");
    scanf("%d", &i);
    Node* move = head->next;
    while (move != NULL)
    {
        if (strcmp(name, move->equipment.name) == 0)
        {
            move->equipment.number -= i;
            system("pause");
            system("cls");
        }
        move = move->next;
    }
    printf("未能找到设备信息!\n");
    system("pause");
    system("cls");
}
//↑↑↑按照品名去出库(删除)一定数量的设备↑↑↑

void writefile(Node* head)
{
    Node* p = NULL;
    FILE* fp = NULL;
    if (!head || !head->next)
        return;
    if ((fp = fopen("Equipment.txt", "w")) == NULL)
        return;
    p = head->next;
    while (p) {
        fwrite(&p->equipment, sizeof(Equipment), 1, fp);
        p = p->next;
    }
    fclose(fp);
}

void readfile(Node* head)
{
    Node* p = NULL;
    FILE* fp = NULL;
    if (!head) return;
    if ((fp = fopen("Equipment.txt", "r")) == NULL)
        return;
    p = head;
    while (p->next) p = p->next;
    while (1) {
        Node* pt = (Node*)malloc(sizeof(Node));
        pt->next = NULL;
        if (fread(&pt->equipment, sizeof(Equipment), 1, fp) != 1) {
            free(pt);
            break;
        }
        p->next = pt;
        p = pt;
    }
    fclose(fp);
}