!283 2022-01-05 22:06 采纳率: 50%
浏览 42
已结题

我用链式存储,想从初始文件中查找数据然后读入另一个文件,但却报错

这是报错图片:

img

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef struct {
    char name[20];
    char leibie[20];
    int number;
    int price;
    int member;
    float time;
    int zhuohao;
}restaurant;
typedef struct listnode {
    restaurant data;//数据域
    struct listnode* next;//指针域
}listnode, * linklist;
linklist l = NULL;
FILE* fp,*d;
char c[1000];
char m[20] = "销售额";
char file[20] = "membership";
char g[20] = "sk";
char filename[20] = "das";
void createlist() {
    l = (listnode*)malloc(sizeof(listnode));
    l->next = NULL;
}
void xiaoshou() {
    createlist();
    listnode* p, * q;
    q = l;
    d = fopen(m, "a+");
    while (true) {
        p = (listnode*)malloc(sizeof(listnode));
        if (fscanf(d, "%d\t%s\t%s\t%d", &p->data.number, &p->data.name, &p->data.leibie, &p->data.price) == EOF) {
            free(p);
            system("pause");
            break;
        }
        p->next = NULL;
        q->next = p;
        q = q->next;
    }
    fclose(d);
}
void diancai() {
    cout << endl << endl;
    cout << "\t\t\t请添加你选择的菜品信息\n";
    int x = 1;
    linklist p, q;
    while (1) {
        int s;
        cout << "\t\t\t请输入菜号:";
        cin >> s;
        linklist g;
        g = l->next;
        if (g == NULL) {
            cout << "\t\t\t没有菜品";
            system("pause");
            return;
        }
        cout << "\t\t\t菜号\t菜名\t\t类别\t价格\n";
        while (g != NULL) {
            if (g->data.number==s) {
                printf("\t\t\t%d\t%s\t%s\t%d\n", g->data.number, g->data.name, g->data.leibie, g->data.price);
                
            }
            g = g->next;
        }
        p->data.number = g->data.number;
        strcpy(p->data.name, g->data.name);
        strcpy(p->data.leibie, g->data.leibie);
        p->data.price = g->data.price;
        if (l->next == NULL) {
            p->next = NULL;
            l->next = p;
        }
        else {
            q = l->next;
            while (q != NULL)
            {
                p->next = NULL;
                q->next = p;
                q = q->next;
            }
        }
        cout << "\t\t\t继续添加输入1,结束输入0:";
        cin >> x;
    }
}
void xiaoshufile() {
    linklist q = l->next;
    d = fopen(m, "a+");
    if ((d = fopen(m, "w")) == NULL) {
        cout << "\t\t\t打开文件失败,";
        system("pause");
        return;
    }
    else {
        while (q != NULL) {
            fprintf(d, "%d\t%s\t%s\t%d\n", q->data.number, q->data.name, q->data.leibie , q->data.price);
            q = q->next;
        }
    }
    fclose(d);
    system("pause");
}
void openfile() {
    createlist();
    listnode* g, * h;
    h = l;
    fp = fopen(filename, "a+");
    while (true) {
        g = (listnode*)malloc(sizeof(listnode));
        if (fscanf(fp, "%d\t%s\t%s\t%d", &g->data.number, &g->data.name, &g->data.leibie, &g->data.price) == EOF) {
            free(g);
            system("pause");
            break;
        }
        g->next = NULL;
        h->next = g;
        h = h->next;
    }
    fclose(fp);
}
void add_message() {
    cout << endl << endl;
    cout << "\t\t\t正在添加菜品信息\n";
    int x = 1;
    linklist g, h;
    while (x) {
        g = (listnode*)malloc(sizeof(listnode));
        printf("\t\t\t正在添加菜品的信息\n");
        cout << "\t\t\t请输入菜号:";
        cin >> g->data.number;
        g = l->next;
        while (g != NULL) {
            if (h->data.number == g->data.number) {
                cout << "\t\t\t菜号重复,重新输入\n";
                cin >> g->data.number;
                h = l->next;
                continue;
            }
            else h = h->next;
        }
        cout << "\t\t\t请输入菜名:";
        cin >> g->data.name;
        cout << "\t\t\t请输入类别:";
        cin >> g->data.leibie;
        cout << "\t\t\t请输入价格:";
        cin >> g->data.price;
        cout << "\n";
        if (l->next == NULL) {
            g->next = NULL;
            l->next = g;
        }
        else {
            h = l->next;
            while (h != NULL)
            {
                if (h->next != NULL)
                {
                    if (g->data.number > h->data.number && g->data.number < h->next->data.number) {
                        g->next = h->next;
                        h->next = g;
                        break;
                    }
                }
                else {
                    g->next = NULL;
                    h->next = g;
                    break;
                }
                h = h->next;
            }
        }
        cout << "\t\t\t继续添加输入1,结束输入0:";
        cin >> x;
    }
}
void print_message() {
    cout << "\n\n\t\t\t显示菜品信息\n";
    linklist g;
    g = l->next;
    cout << "\t\t\t菜号\t菜名\t\t类别\t价格\n";
    if (g == NULL) {
        cout << "\t\t\t没有数据!";
        system("pause");
        return;
    }
    while (g != NULL) {
        printf("\t\t\t%d\t%s\t%s\t%d\n", g->data.number, g->data.name, g->data.leibie, g->data.price);
        g = g->next;
    }
    system("pause");
}
void save_file() {
    linklist h = l->next;
    if ((fp = fopen(filename, "w")) == NULL) {
        cout << "\t\t\t打开文件失败,";
        system("pause");
        return;
    }
    else {
        while (h != NULL) {
            fprintf(fp, "%d\t%s\t%s\t%d\n", h->data.number, h->data.name, h->data.leibie, h->data.price);
            h = h->next;
        }
    }
    fclose(fp);
    cout << "\t\t\t保存成功,";
    system("pause");
}
int main() {
    cout << "\n\n\t\t\t1.初始文件中信息的添加\n";
    cout << "\n\n\t\t\t2.从初始文件往另一个文件里进行信息的添加\n";
    int x;
    while (scanf("%d", &x) != EOF) {
        switch (x) {
        case 1:
            openfile();
            add_message();
            print_message();
            save_file();
            break;
        case 2:
            xiaoshou();
            openfile();
            diancai();
            xiaoshufile();
            break;
        }
    }
}

希望有人可以帮帮我,改一下错误。

我想要达到的结果
  • 写回答

2条回答 默认 最新

  • 正在学C++ 2022-01-06 22:30
    关注
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    typedef struct {
        char name[20];
        char leibie[20];
        int number;
        int price;
        int member;
        float time;
        int zhuohao;
    }restaurant;
    typedef struct listnode {
        restaurant data;//数据域
        struct listnode* next;//指针域
    }listnode, * linklist;
    
    linklist l = NULL;  //菜单列表
    linklist l2 = NULL;  //已售出列表即销售额
    FILE* fp,*d;
    char c[1000];
    char m[20] = "xiaoshou.txt";    //尽量不要中文文件名 这个文件理论上是在当前目录下
                                    //但是实际是在项目的debug文件目录下。如果找不到,可换成绝对路径
    char file[20] = "membership";
    char g[20] = "sk";
    char filename[20] = "das";
    
    int menu();
    void InitList(linklist &l){
        l = (listnode*)malloc(sizeof(listnode));
        l->next = NULL;
    }
    void xiaoshou() {
        listnode* p, * q;
        q = l2;
        if ((d = fopen(m, "a+")) == NULL) {
            cout << "\t\t\t打开文件失败,";
            system("pause");
            return;
        }
        while (true) {
            p = (listnode*)malloc(sizeof(listnode));
            if (fscanf(d, "%d\t%s\t%s\t%d", &p->data.number, p->data.name, p->data.leibie, &p->data.price) == EOF) {//////////////
                free(p);
                break;
            }
            p->next = NULL;
            q->next = p;
            q = q->next;
        }
        fclose(d);
    }
    void diancai() {
        cout << "\n\t\t\t请添加你选择的菜品信息\n";
        int x = 1;
        linklist p, q;
        while (x) {
            int s;
            cout << "\t\t\t请输入菜号:";
            cin >> s;
            linklist g;
            g = l->next;
            if (g == NULL) {
                cout << "\t\t\t没有菜品";
                system("pause");
                return;
            }
            while (g != NULL) {
                if (g->data.number==s) {
                    cout << "\t\t\t菜号\t菜名\t类别\t价格\n";
                    printf("\t\t\t%d\t%s\t%s\t%d\n", g->data.number, g->data.name, g->data.leibie, g->data.price);
                    break;
                }else g = g->next;
            }
            if(g==NULL) cout << "\t\t\t没有菜品";
            else{
                InitList(p);    //p需要存储卖出的菜,所以需要初始化开辟存储空间
                p = g;
                p->next = NULL;
                q = l2;   //q自己不存储信息,它只是指向某一个已存储信息的节点所以不初始化
                while (q->next != NULL) q = q->next;
                q->next = p;
            }
            cout << "\n\t\t\t继续添加输入1,结束输入0:";
            cin >> x;
        }
    }
    void xiaoshufile() {
        linklist q = l2->next;
        if ((d = fopen(m, "w")) == NULL) {
            cout << "\t\t\t打开文件失败,";
            system("pause");
            return;
        }
        else {
            while (q != NULL) {
                fprintf(d, "%d\t%s\t%s\t%d\n", q->data.number, q->data.name, q->data.leibie , q->data.price);
                q = q->next;
            }
        }
        fclose(d);
        cout << "\t\t\t保存成功,";
        system("pause");
        menu();
    }
    void openfile() {
        listnode* g, * h;
        h = l;
        fp = fopen(filename, "a+");
        while (true) {
            g = (listnode*)malloc(sizeof(listnode));
            g->next = NULL;
            if (fscanf(fp, "%d\t%s\t%s\t%d", &g->data.number, g->data.name, g->data.leibie, &g->data.price) == EOF) {   ////////////////////
                free(g);
                break;
            }
            h->next = g;
            h = h->next;
        }
        fclose(fp);
    }
    void add_message() {
        cout << "\n\t\t\t正在添加菜品信息\n";
        int x = 1;
        linklist g, h;
        while (x) {
            g = (listnode*)malloc(sizeof(listnode)); g->next = NULL; ///////////////////
            printf("\t\t\t正在添加菜品的信息\n");
            cout << "\t\t\t请输入菜号:";
            cin >> g->data.number;
            while (1){
                h = l->next;
                while (h!=NULL) {
                    if(h->data.number == g->data.number){
                        cout << "\t\t\t菜号重复,重新输入\n";
                        cin >> g->data.number;
                        break;
                    }
                    h = h->next;
                }
                if(h==NULL) break;
            }
            cout << "\t\t\t请输入菜名:";
            cin >> g->data.name;
            cout << "\t\t\t请输入类别:";
            cin >> g->data.leibie;
            cout << "\t\t\t请输入价格:";
            cin >> g->data.price;
            cout << "\n";
            if (l->next == NULL) {
                l->next = g;
            }
            else {
                h = l;
                while (h->next!=NULL)
                {
                    h = h->next;
                    if (h->next != NULL && g->data.number >= h->data.number && g->data.number <= h->next->data.number) {
                        g->next = h->next;
                        h->next = g;
                        break;
                    }else if(h->next == NULL) {
                        break;
                    }
                }
                h->next = g;
            }
            cout << "\t\t\t继续添加输入1,结束输入0:";
            cin >> x;
        }
    }
    void print_message() {
        cout << "\n\n\t\t\t显示菜品信息\n";
        linklist g;
        g = l->next;
        cout << "\t\t\t菜号\t菜名\t\t类别\t价格\n";
        if (g == NULL) {
            cout << "\t\t\t没有数据!";
            system("pause");
            return;
        }
        while (g != NULL) {
            printf("\t\t\t%d\t%s\t%s\t%d\n", g->data.number, g->data.name, g->data.leibie, g->data.price);
            g = g->next;
        }
        system("pause");
    }
    void save_file() {
        linklist h = l->next;
        if ((fp = fopen(filename, "w")) == NULL) {
            cout << "\t\t\t打开文件失败,";
            system("pause");
            return;
        }
        else {
            while (h != NULL) {
                fprintf(fp, "%d\t%s\t%s\t%d\n", h->data.number, h->data.name, h->data.leibie, h->data.price);
                h = h->next;
            }
        }
        fclose(fp);
        cout << "\t\t\t保存成功,";
        system("pause");
        menu();
    }
    int menu(){
        cout << "\n\t\t\t1.初始文件中菜品信息的添加";
        cout << "\n\t\t\t2.从菜品文件往销售额文件添加信息";
        cout << "\n\t\t\t3.退出\n";
        int x;
        cin>>x;
        switch(x){
            case 1:
                openfile();
                add_message();
                print_message();
                save_file();
                break;
            case 2:
                xiaoshou();
                openfile();
                diancai();
                xiaoshufile(); //把销售出去的写入文件
                break;
            default:
                return 0;
        }
        return 0;
    }
    int main() {
        InitList(l);    //这个链表是存储菜品信息
        InitList(l2);   //这个链表是存储销售信息
        menu();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月7日
  • 已采纳回答 1月7日
  • 创建了问题 1月5日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?