运行功能4显示所有设备时出错,这怎么办


#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);//对删除设备函数进行声明

void save(Node* head);
void load();


int main()//主函数
{
    load();
    //创建头节点
    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':
            load();
            inputEquipment(head);//增加设备 
            break;
        case'2':
            deleteEquipment(head);//删除一定数量的设备
            break;
        case'3':
            findEquipment(head);//查找设备
            break;
        case'4':
            //load();
            printEquipment(head);//显示所有设备
            break;
        case'5':
            save(head);
            break;
        case'6':
            load(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("                                          ***     5.将链表信息存入文件    ***\n");
    printf("                                          ***     6.打开并读取文件内容并显示    ***\n");
    printf("                                          ****************************\n");
    printf("********************设定规则:有3种设备:TV DVD TVwithDVD;其中TV与DVD都为500元,TVwithDVD为800元***********************\n");
    printf("请选择>");
}         
//↑↑↑菜单函数,打印菜单↑↑↑

void inputEquipment(Node*head)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    Node* q;//新建一个Node型指针q 
    Node* r;//用来遍历区间
    r = head;//注意插入的时候,指针从head开始 
    while (r->next != NULL)
    {//当r不是尾指针的时候 
        r = r->next;//循环 
    }
        q = newNode;//指向一个申请的节点 
    printf("请输入设备编号,名称,价格,数量");
    scanf("%d%s%d%d", &q->equipment.equNum, q->equipment.name, &q->equipment.price, &q->equipment.number);//输入的信息就存在刚才申请的空间里
    r->next = q;//插入 
    q->next = NULL;//赋空 
    r = q; //换值 
    system("pause");
    system("cls");
}
//↑↑↑输入信息函数,即增加设备↑↑↑

void printEquipment(Node* head)
{
    Node* q = head->next;
    while (q != NULL)
    {
        printf("编号:%d 名称:%s 价格:%d 数量:%d\n", q->equipment.equNum, q->equipment.name, q->equipment.price, q->equipment.number);
        q = q->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");
}
//↑↑↑按照品名去出库(删除)一定数量的设备↑↑↑



char filename[50];
void save(Node* head)
{//创建并将链表信息存入文件
    //char filename[50];
    printf("please input filename: ");
    scanf("%s", filename); /*输入文件所在路径及名称*/
    FILE* fp;
    Node* move= head->next;
    if (!(fp = fopen(filename,"w")))
    {
        printf("打开失败");
        exit(1);
    }
    else
    {
        printf("打开save成功\n");
    }
    while (move != NULL)
    {
        fprintf(fp, "%d\n%s\n %s\n%d\n", move->equipment.equNum);
//        fprintf(fp, "%s\n", move->equipment.name);
//        fprintf(fp, "%s\n",, move->equipment.number);
//        fprintf(fp, "%d\n", move->equipment.price);
        move = move->next;
    }
    printf("成功\n");
    fclose(fp);

}



void load(Node*head) 
{//打开并读取文件内容并显示
    FILE* fp=0;
    int n = 2;
    if (!(fp = fopen("C:\\Users\\陈康康\\source\\repos\\自建设备管理系统\\自建设备管理系统\\ccc","r")))
    {
        printf("打开失败");
        exit(-1);
    }
    else {
        printf("load成功\n");
    }
    Node* fresh = malloc(sizeof(Node));//申请一块空间,让用户输入的信息存储到这个空间里
    fresh->next = NULL;//这个新的节点后面是没有数据的,即指向空
    while(1)
//    while (fscanf(fp, " %d%s%d%d", &fresh->equipment.equNum,fresh->equipment.name, &fresh->equipment.number, &fresh->equipment.price) != EOF)
    {
        fresh = (Node*)malloc(sizeof(Node));
        fscanf(fp, "%d%s %d%d\n", &fresh->equipment.equNum , fresh->equipment.name, &fresh->equipment.number ,&fresh->equipment.price);
//        fscanf(fp, "%s\t\n", &q1->equipment.name);
//        fscanf(fp, "%d\t\n", &q1->equipment.number);
//        fscanf(fp, "%d\t\n", &q1->equipment.price);
        printf("%d", fresh->equipment.equNum);
        printf("%s", fresh->equipment.name);
        printf("%d", fresh->equipment.price);
        Node* move = head;
        while (move->next != NULL)
        {
            move = move->next;
        }
        //将设备信息插入到尾部
        move->next = fresh;
        if (feof(fp))
            break;
//        p1->next = q1;//尾插
//        q1->next = NULL;
//        p1 = q1;
        n--;
    }
    printf("load结束");
    fclose(fp);

}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/396692671876159.png "#left")

根据你提供的代码,设备信息结构体定义中的equNum、name、price和number都是按照正确的数据类型定义的。在运行功能4时出错,可能有以下几个原因:

  1. 链表中没有任何设备信息节点
    如果链表中没有任何设备信息节点,则功能4尝试访问链表时会出现错误。因此,在添加设备信息节点之前,请确保链表中至少有一个设备信息节点。
  2. 打印设备信息时出现了格式化字符串问题
    当我们使用printf函数打印字符串时,格式化字符串中的%符号和转换字符之间需要有一个字符,例如% d和% s。否则,编译器会将两个字符合并为一个字符,导致程序崩溃或输出错误。请确保在格式化字符串中正确使用%符号和转换字符。
  3. 链表节点指针问题
    当使用链表进行数据操作时,需要特别注意节点指针的位置和方向。例如,在功能4中,我们需要使用指向链表头节点的指针变量head来访问整个链表,如果在对链表节点进行操作时使用了错误的指针,就会出现访问内存错误的问题。请确保在对链表进行操作时使用正确的指针。
  4. 文件读写操作问题
    如果在保存和读取链表信息时出现错误,则会导致链表中的数据无法正确保存或读取,从而导致功能4出现错误。请确保在进行文件读写操作时,文件路径和文件名正确,并且文件已经正确打开和关闭。

建议你逐个排查上述可能存在的问题,找到具体出错原因后再进行修正和调试。望采纳!

你得先说清楚,在print输出所有设备之前,你都干了啥?增加、修改、删除设备你都干了哪些。输出函数本身没啥问题
31行的load函数申明是无参的,可216行load函数实现却有参数,你咋通过编译的呢?