栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

链式存储从一个文件往另一个文件中读取指定信息

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

链式存储从一个文件往另一个文件中读取指定信息

对于链式存储来说,它的好处就是每一个元素可以存储在任意位置,只需要通过指针将相互直接关联的元素链接起来就好。代码部分有信息的写入,信息的读入信息的保存,当然,我最开始一直在链表的初始化犯迷糊,最后经人指点。也成功解决了这个问题,下面代码是关于这个问题的呈现。

关于程序的准备如下:

#include
#include
#include
using namespace std;
typedef struct {
    char name[20];
    char leibie[20];
    int number;
    int price;
}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";
void InitList(linklist& l) {
    l = (listnode*)malloc(sizeof(listnode));
    l->next = NULL;
}
int menu();

之后就是文件的创建,读取,保存,当然我觉的显示这部分没有什么必要,可以在VS中的文件直接查看,但我还是带上这部分了。

关于第一个文件信息的录入如下:

void openfile() {
    listnode* g, * h;
    h = l;
    fp = fopen(filename, "a+");
    while (true) {
        g = (listnode*)malloc(sizeof(listnode));
        g->next = NULL;
        if (fscanf(fp, "%dt%st%st%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 << "nttt正在添加菜品信息n";
    int x = 1;
    linklist g, h;
    while (x) {
        g = (listnode*)malloc(sizeof(listnode)); g->next = NULL; 
        printf("ttt正在添加菜品的信息n");
        cout << "ttt请输入菜号:";
        cin >> g->data.number;
        while (1) {
            h = l->next;
            while (h != NULL) {
                if (h->data.number == g->data.number) {
                    cout << "ttt菜号重复,重新输入n";
                    cin >> g->data.number;
                    break;
                }
                h = h->next;
            }
            if (h == NULL) break;
        }
        cout << "ttt请输入菜名:";
        cin >> g->data.name;
        cout << "ttt请输入类别:";
        cin >> g->data.leibie;
        cout << "ttt请输入价格:";
        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 << "ttt继续添加输入1,结束输入0:";
        cin >> x;
    }
}
void print_message() {
    cout << "nnttt显示菜品信息n";
    linklist g;
    g = l->next;
    cout << "ttt菜号t菜名tt类别t价格n";
    if (g == NULL) {
        cout << "ttt没有数据!";
        system("pause");
        return;
    }
    while (g != NULL) {
        printf("ttt%dt%st%st%dn", 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 << "ttt打开文件失败,";
        system("pause");
        return;
    }
    else {
        while (h != NULL) {
            fprintf(fp, "%dt%st%st%dn", h->data.number, h->data.name, h->data.leibie, h->data.price);
            h = h->next;
        }
    }
    fclose(fp);
    cout << "ttt保存成功,";
    system("pause");
    menu();
}

而这一部分就是这个问题的关键,对写入文件信息的查找并读入另一个文件,当时我的思想是,当查找到这个文件时,把这个节点的信息赋值给另一个链表的节点,但因为这个节点并没有初始化,所以这是行不通的,所以在准备工作中,解决了具体的问题,而其中的保存,创建,读取文件从上面的代码就可以了解,主要看diancai()这个区域的代码。具体的代码如下:

void xiaoshou() {
    listnode* p, * q;
    q = l2;
    if ((d = fopen(m, "a+")) == NULL) {
        cout << "ttt打开文件失败,";
        system("pause");
        return;
    }
    while (true) {
        p = (listnode*)malloc(sizeof(listnode));
        if (fscanf(d, "%dt%st%st%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 << "nttt请添加你选择的菜品信息n";
    int x = 1;
    linklist p, q;
    while (x) {
        int s;
        cout << "ttt请输入菜号:";
        cin >> s;
        linklist g;
        g = l->next;
        if (g == NULL) {
            cout << "ttt没有菜品";
            system("pause");
            return;
        }
        while (g != NULL) {
            if (g->data.number == s) {
                cout << "ttt菜号t菜名t类别t价格n";
                printf("ttt%dt%st%st%dn", g->data.number, g->data.name, g->data.leibie, g->data.price);
                break;
            }
            else g = g->next;
        }
        if (g == NULL) cout << "ttt没有菜品";
        else {
            InitList(p);    //p需要存储卖出的菜,所以需要初始化开辟存储空间
            p = g;
            p->next = NULL;
            q = l2;   //q自己不存储信息,它只是指向某一个已存储信息的节点所以不初始化
            while (q->next != NULL) q = q->next;
            q->next = p;
        }
        cout << "nttt继续添加输入1,结束输入0:";
        cin >> x;
    }
}
void xiaoshufile() {
    linklist q = l2->next;
    if ((d = fopen(m, "w")) == NULL) {
        cout << "ttt打开文件失败,";
        system("pause");
        return;
    }
    else {
        while (q != NULL) {
            fprintf(d, "%dt%st%st%dn", q->data.number, q->data.name, q->data.leibie, q->data.price);
            q = q->next;
        }
    }
    fclose(d);
    cout << "ttt保存成功,";
    system("pause");
    menu();
}

最后一部分为程序的运行部分,就不做解释了,都能看懂。

int menu() {
    cout << "nttt1.初始文件中菜品信息的添加";
    cout << "nttt2.从菜品文件往销售额文件添加信息";
    cout << "nttt3.退出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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/698196.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号